Im using Android Studio, trying set text to Text View but every time i do this
TextView test = (TextView) findViewById(R.id.testView);
test.setText("fds");
on Android Studio i see Cannot resolve symbol 'setText'.
Can someone tell me what im doing wrong?
setText() will accept String which you have passed, so there is no issue in that.
One possible reason for this might be you have not written your code inside onCreate() or onCreateView().
If it is Activity you need to use these lines after setContentView() in onCreate().
If it is Fragment you need to use these lines after inflating your view in onCreateView().
Second reason, you might be having one more test variable of different type like String or something else
Related
I want to generate a listview from sqlite database but the
LOGCAT shows error.
What does this below error mean?
09-23 12:01:20.354: E/AndroidRuntime(3682): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.abhishekp.listproduct/com.abhishekp.listproduct.List}: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.ListView
Its Happen only because of..two possibilities.
1) If you are using TextView in xml file...
and try to initialize it with.
TextView tv=(ListView)findViewbyId(R.id.textView);
and if it is right then change it with
TextView tv=(TextView)findViewbyId(R.id.textView);
2) May be you are trying to put id of listView instead of text view.
like
TextView tv=(TextView)findViewbyId(R.id.listView);
if it is then should change
TextView tv=(TextView )findViewbyId(R.id.textView );
And if it is all good then try to clean your project and rebuild it...
Thanks!!
From your logcat its clearly showing that you are trying to cast your TextView in ListView. Check you may have declared your TextView i.e.
TextView tv=(ListView)findViewbyId(R.id.textView);
or may be you are referring the id of your ListView in your java file.
TextView tv=(ListView)findViewbyId(R.id.listView); //id defined is of listview
Eclipse tends to mess up your resources every now and then. This leads to some odd behavior such as strings and images being swapped all over your app, and more commonly classCastException(s), which happen when Eclipse switches your Views' ids around.
Just clean your project.It will work fine.
I have some problems with the TabHost and ViewFlipper.
Here are the ViewFlipper as I expect the answer to this will also do the job in the TabHost.
I would like to have a Custom Dialog shown when the user reach a certain stage, but I can not figure out which Context to hand it?
final Dialog congratsDialog = new Dialog(MyActivity.this);
congratsDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
congratsDialog.setContentView(R.layout.congrats_dialog);
TextView name = (TextView) congratsDialog.findViewById(R.id.congratsDialogName);
name.setText(player.getName());
This will result in a NullPointerException in the line were I try to setText.
I have also tried flipper.getContext(), getBaseContext(), getApplicationContext() and have also tried other crazy thing but every time I get a NullPointerException
setContentView() to dialog is trade off over android version if you are using android 2.0 or less it would not work use versions 2.0 or above for this function. Otherwise if you want to do for all version then use setContentView(View) where View is from xml layout of congrats dialog after inflating it.
Please try this and let me know if you got solution.
Well basicly I have a textview and when the application is created it sets a string as the textviews text not hard, but I get a force close error when I run the app on my phone.
TextView sdcard=(TextView)findViewById(R.id.sd_textview);
sdcard.setText(R.string.not_mounted);
Then I have a error on a togglebutton also
ToggleButton silent=(ToggleButton)findViewById(R.id.silentbutton);
silent.setChecked(false);
And I have errors for all my other buttons/textviews can anyone help, please?!
EDIT:
I cant post pics because I am a new member, :(
Link to imgshack http://imageshack.us/photo/my-images/849/unledggp.png/
If code for the whole textview snippet.
if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_UNMOUNTED)) {
TextView sdcard=(TextView)findViewById(R.id.sd_textview);
sdcard.setText(R.string.not_mounted);
}
OnCreate Function
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
checkSD();
checkRing();
checkWifi();
checkBt();
}
Look for all instances of sd_textview and make sure the one that you're trying to reference is a TextView. If you want more clarity you can debug your code and see what object is actually being returned by not casting into a TextView:
View sdcard = findViewById(R.id.sd_textview); //debug this
//you can also log the View object to see the type
Log.d("Test", "" + sdcard);
Looking at your error log (assuming its the right error log) you have a ClassCastException in the checkWifi method. Edit your question and include ALL of the onCreate method and all of the checkWifi method, but I expect you are using the same id for multiple views.
Two things I can think of (although seeing more code would help).
Make sure you have called setContentView(R.layout.main) (or whatever your layout file is called). Do this BEFORE any attempt to use findViewById(...).
Secondly sdcard.setText(R.string.not_mounted); in this statement R.string.not_mounted is a resource ID (int) and not a string. You would need to use...
sdcard.setText(getString(R.string.not_mounted));
I have no idea why this doesn't work. The TextView is defined from an tag in the view. The base TextView doesn't have text set and I want to set it in the View on display.
I have tried placing the below in onCreate and onStart but it doesn't seem to work. The last two lines are just for debugging. I can verify that the header does get the text. The thing is, the TextView doesn't actually get updated. Any ideas?
TextView header=(TextView) findViewById(R.id.acheader);
header.setText(R.string.accounts);
header.invalidate();
header=(TextView) findViewById(R.id.acheader);
String blah=(String) header.getText();
Try again removing the text in 4th line
header=(TextView) findViewById(R.id.acheader);
header.invalidate() is not needed.
Instead of String blah = (String) header.getText() try
String blah = heager.getText().toString();
And why are you verifying a "setText()" on text view using code? Why can't you check the
actual output?
The above code might not work the way you are trying to use it, because the redraw of text view is handled by the framework and generally it tries to group item updates (Dirty rectangles to be specific) and update them all at once. It may do it well after your function exits, Try to validate visually, thats the best way.
I have an activity in my app, when i call the line
EditText username = (EditText)findViewById(R.id.usernameText);
the app crashes, why is this?
The view has not been inflated, or you are missing
setContentView(R.layout.layout_file);
Yeah either setContentView hasn't been called in onCreate of the activity, or else the view you are trying to get isn't an EditText. What is the Exception you are getting? If its ClassCast then its not an EditText.
Things to check as no more information is provided:
R.id.usernameText is a valid identifier in your layout
Your layout has been set as a content view
Also, if you can edit the post and add the log of what's messing up, it will be more easy to reply.