I am not sure if this is an Android issue or a AndroidStudio issue. Why is this valid:
TextView currentItem
currentItem = (TextView)findViewById(R.id.myText);
currentItem.setText("text");
But this is not (cannot resolve method setText(Java.Lang.String))
(TextView)findViewById(R.id.myText).setText("text");
Not a big deal, but I have some code that requires a lot of text updates. It would be much cleaner if I could get this in one line.
findViewById returns a generic 'View', hence you have to cast it to TextView before you can set the text. In your piece of code you are trying to invoke setText on the 'View' object rather than the 'TextView' object.
Try this :
((TextView)findViewById(R.id.myText)).setText("text");
Try this please :
((TextView)findViewById(R.id.myText)).setText("text");
findViewById returns a View, so you don't have setText from TextView yet. You have to cast first to TextView by putting parenthesis around findViewById and then call setText like this:
((TextView)findViewById(R.id.myText)).setText("text");
Also, this is a Java thing, not Android nor Android Studio thing.
Related
Hey guys I am new in programming. Trying out something in Android Studio (Kotlin)
I have looked for this but didn't find an answer. Button
Mostly I use Button.findViewById<Button>(R.id.Button) but sometimes it gives me error and I have to write it like Button = findViewById(R.id.Button) as Button
Can someone tell me where (or what) is the difference?
with kotlin : you don't must use findViewById. you can use direct id ex: btnSave.text="abc"
If code show error, you select [btnSave] and click [Alt + Enter] to import lib.
findViewById search a View that has the Id you give inside the view you call this method with.
So when you do Button.findViewById(R.id.btn) it should never work because Button is a class and not an instance of view.
When you do myButton.findViewById(R.id.btn) it looks inside myButton, that is an instance of view, if there is a view having btn has id. If there is it return it, otherwise it returns null.
When you do findViewById(R.id.btn) You call this method directly from inside a custom view code. Often it's inside an Activity. Then it looks in the layout of this activity if there is a View having btn has ID.If there is it return it, otherwise it returns null.
I have been trying for days to find a solution to my problem, but now I've decided to try to ask you. I'm a noob for programming Android, so please forgive me.
I have a main Activity with a Listview in it.
I am using a Simplecursoradapter to feed it with information from my database and a customized layout I made as an XML.
my problem is, I want to change some of the terms or the units that I used in this custom layout, which is not feed through the database.
But if I use settext to the TextViews in there it wont run, the app will crash, because my setContentView is set to another layout for this class, I guess. I have been looking at inflaters, trying to see if I could change the XML programmable, use a string from String.xml and change that. But as far as I can see these are not an option. Later I found this code on StackOverflow
Activity activity = (Activity)getContext();
TextView t = (TextView)activity.findViewById(R.id.txtDisponible);
t.setText("E-ticket validado");
But I can't get this to work because the first line isn't working for me. The Activity won't give me the getcontext method. And even if I did get this to work, I wouldn't know if this would work. Could you guide me in which direction I need to go? As of now, I don't have a class for my ListView, is that the way to go. I want to keep this as simple as I can.
Thank you for your help.
EDIT 1:
Okay i tried to put this in my code:
LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.item_list, null);
TextView t = (TextView) view.findViewById(R.id.kmListview);
t.setText("bok");
The app doesn't crash, but it doesn't change the textview either.
Activity extends ContextWrapper class and you should use this or getBaseContext() because there is no method like getContext() which extends ContextWrapper class.
So use this as Context or getBaseContext() in your Actitiy and I think your problem will be resolved.
I started with a widget and I'm trying to use a TextView in my Main.xml .So I just add one with drag and drop but I dont know how to find it with the code. In a normal Android Application I used
TextView text = (TextView)findViewById(R.id.name);
and then
text.setText("Blabla");
But when I try it in a Widget I get a error that 'findViewById' is undefined for this type... Is there any alternative ? How do I have to declare it ?
Thanks !
For a widget you have to use RemoteViews. http://developer.android.com/reference/android/widget/RemoteViews.html
RemoteViews has several methods to set the values of the containing views, e.g. setTextViewText(). http://developer.android.com/reference/android/widget/RemoteViews.html#setTextViewText%28int,%20java.lang.CharSequence%29
I was trying to build a typical ListView using the default fragment view in Android - so I need to use setContentView() first to after get the appropriate ListView in that layout. However, I keep getting this error:
Cannot make a static reference to the non-static method
setContentView(int) from the type Activity
I understand this error but do not know how to fix it here (I cannot just go and transform it to static). I am sure my layout name is correct (R.layout.menuList). I am executing this in an AsyncTask under the onPostExecute() section (so it is the same thread as the UI). What am I missing here?
My first thought: Although you've not mentioned but since its complaining about static reference, I assume that you are trying YourActivity.setContentView(R.layout.menuList); ?
Instead try using YourActivity.this.setContentView(R.layout.menuList);. You need correct context.
I run a setText command inside of a fragment activity to try and set the text of a textView in the parent activity, but it's not working. Any ideas?
TextView text = (TextView) getView().findViewById(R.id.status);
text.setText("Text from a fragment");
I don't get an error in eclipse, but I get a null pointer exception during runtime. Of course it happens at the line where I setText. Any ideas on how to do this?
Use getActivity(). It will cause the findViewById() method to start the search at the base activity and bubble up until it finds your "r.id.status".
TextView text = (TextView) getActivity().findViewById(R.id.status);
text.setText("Text from a fragment");
Yes your NPE will probably be because R.id.status is probably not defined in the fragment's view.
getView() will return the view that is generated in your Fragment's onCreateView() method.
Are you trying to set a TextView in your Activity from a (secondary) FragmentActivity or trying to set a TextView in your FragmentActivity from a Fragment?
If it's the 1st option, you'll want to do something like use a message handler and pass the 2nd Activity a message. I don't think this is what you're asking though.
If you're wanting to set a TextView from a Fragment, the general way to do that is to define an interface on your FragmentActivity with a method (updateText() for example) and get the Fragment to call the method. The Activity then handles the TextView update, which works nicely because it can call getView() which will return the view you're looking for. It's similar to my answer posted here