In my app I have 3 radiobuttons on top of the view. When i choose one the 'body' underneath that changes to the XML-view i defined.
(If you want more information + pics, I asked a question about this earlier: Dynamically change view inside view in Android)
Now i want to call and change the text of the buttons and edittext's from the different views.
When I do btnAutopech = (Button) findViewById(R.id.btnAutopech); it's gives an NullPointerException.
How can I do this?
Try this.......
LayoutInflater layoutInflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout ll= new LinearLayout(context);
ll=(LinearLayout)layoutInflater.inflate(R.layout.gegevens_verzekeringen, ll);
btnAutopech = (Button) ll.findViewById(R.id.btnAutopech);
Thanks.........
Depends on how you solved the last question you linked to. If your inflating new views into the area you want to change then you won't be able to retreive a reference to them using finViewById. I.e. they don't actually exist.
When you re-display your view, within which you want to display text based, on your other views outcome, you would have to re-assign the text when you re-attach/display your view. You could then assign text from a member variable of the class, or perhaps from sharedPreferences or a contentProvider.
EIther way, this sort of all depends on how you've solved the issue of your original question and when you attach/remove your views.
To summerise:
It looks like your removing your other views when you visit other views, so store your data in a member variable that persists.
attach view A
got to view B
Click button or something in view B and update member variable used by view A
Go to view A (Removing view B and attaching view A)
set text on view A from member variable (updated as a result of clicking button in view B for example)
Related
For an Android App, I'm using a GridView and extending BaseAdapter to organize its contents. For the function getView that I override in my extended BaseAdapter class, I create a LinearLayout, which I attach an ImageView and 3 TextViews to. Now, I need to implement convertView, but because I created my views programmatically, I didn't think I can use findViewById to find these child views to change their properties (like text and bitmaps).
I had the idea of assigning a unique ID pertaining to different types of views for each one when I create them (example: give my name textview the id of 1, description textview the id of 2, etc), but I was not sure if the ids have to be unique among every view, whether they're the same kind of view or not.
Ultimately, how do I find a child view that's part of a linearlayout view which were all created programmatically?
You can get children of LinearLayout via getChildAt() but it's hard to distinguish what child exactly you get.
I think assigning IDs is better way. You can assign IDs to your views and later get views via findViewById(). IDs doesn't need to be unique for each LinearLayout.
Something like this:
// Give more sensible names to ID's
int IMAGEVIEW_ID = 0;
int TEXTVIEW1_ID = 1;
int TEXTVIEW2_ID = 2;
int TEXTVIEW3_ID = 3;
imageView.setId(IMAGEVIEW_ID);
textView1.setId(TEXTVIEW1_ID);
textView2.setId(TEXTVIEW2_ID);
textView3.setId(TEXTVIEW3_ID);
...
// somewhere later
ImageView imageView = (ImageView) convertView.findViewById(IMAGEVIEW_ID);
I have a poll of ID's (ids.xml), and I assign id's for views I create dynamically. Now my question is pretty simple - assume I create a new view and assign it an id with setId() in conjuction with R.id.uniqueId. Later on, can I access the view with findViewById(R.id.uniqueId)?
If so, what could be the reason it returns null?
Here is a toy example: UPDATED
LinearLayout l = new LinearLayout(this);
l.setId(R.id.mId);
setContentView(l); //i see on screen the views added to 'l'
l = (LinearLayout) findViewById(R.id.mId); //it returns null :(
How come it does not register\map the assgined ID to the view it was assigned?
Does your desired view have a parent view that you could use to call parent.findViewById on? That may help narrow your problem down.
One thing I noticed that's missing from your brief example: you need to make sure you're adding the new LinearLayout to the view hierarchy before you will be able to find it with findViewById:
findViewById(R.id.parent).addView(l)
You can also use the hierarchy viewer to take a look and see if everything's being set up properly.
encountered this many times, try cleaning your project by making a clean, Also check findViewById() returns null for custom component in layout XML, not for other components
From what you have given us it appears that the view is never added to the Activity's view. This is necessary as findViewById uses the activity's view as a parent to find a child view with that Id.
If you are creating the view you can always create it as a member variable and refer to it that way if you are going to be adding it to a view at a later time. Please note that the view must be added after the id has been set.
We are creating an app with two main views: sView and sViewSettings. If the Android Back button is pressed we want an if statment to check if the current view is set to sView settings, if it is then call the sView.
Already have a listener setup for the back button just need it to call the if statement to check the current view.
Have already tried
if (this.findViewById(android.R.id.content) == sViewSettings)
Any ideas on this?
Thank you for Reading,
Travis
The view with id android.R.id.content is a FrameLayout holding your content view. Try this:
ViewGroup contentFrame = (ViewGroup) findViewById(android.R.id.content);
if (contentFrame.getChild(0) == sViewSettings) { ... }
However, I suggest a slightly different approach: use a ViewSwitcher (or any kind of ViewAnimator) to flip between the two main views and keep track in your code of which one is on display.
EDIT: If you want to keep your layouts loaded separately, you can assign an id (the same one) to the root view of each layout and then retrieve the content view directly using findViewById.
In my \drawable-mdpi folder, I have an image named: test.jpg
In my main.xml file, in my LinearLayout section, I have:
<ImageView
android:id="#+id/test_image"
android:src="#drawable/test"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
In my src folder, I have only 1 file, HelloAndroidActivity.java with only the following method:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageView image = new ImageView(this);
image = (ImageView) findViewById(R.id.test_image);
setContentView(image);
}
This seems to be correct, yet whenever I try to run it, I get The application HelloAndroid (process xxxxx) has stopped unexpectedly. Please try again.
The strange part is it previously did display the image, but now it won't and I don't know why.
Also, when I comment out the ImageDisplay code, and replace it with TextDisplay code. i.e.
TextView tv = new TextView(this);
tv.setText("Does this work?");
setContentView(tv);
It does display the text.
Edit: was asked to post logcat. Link to pastebin.
This is going to sound harsh, but this is when I take the knife out of your hand and say you should do a little more reading or whatever:
setContentView(R.layout.main);
ImageView image = new ImageView(this);
image = (ImageView) findViewById(R.id.test_image);
setContentView(image);
You've set the content view to an xml layout. That's cool. Then you create a new ImageView instance in code, which is OK but probably a little over your skill level just now.
You then overwrite the ImageView you just created with one you pulled from the layout that Android has inflated for you from your original setContentView call. This makes no sense. Just do this.
setContentView(R.layout.main);
ImageView image = (ImageView) findViewById(R.id.test_image);
Finally, you overwrite the content view for the whole Activity with the image reference you pulled from within the content view for the current Activity. Do not do this. It makes no sense. My guess is that your exception is either that you're resetting the content view, or that you're trying to set the content view with a view that already exists in another view.
Are you trying to dynamically set the image that shows up? If not, if you're just trying to "show" the image, do this.
setContentView(R.layout.main);
That's it.
setContentView and findViewById don't do what you think they do.
setContentView sets the given view as the top-level, root view of your activity. Usually you set it to a layout that defines a hierarchy of many nested views. If you give it a layout resource, that layout will be inflated into new views. If you give it an existing view, that existing view instance will be used.
Activity#findViewById performs a tree search across the current view hierarchy for your activity's window, starting at the root. If it can find a view somewhere in the hierarchy with the given id, that view is returned. If it can't be found the function returns null.
So with those things in mind, let's step through your code:
setContentView(R.layout.main); - Inflates your main layout and sets it as your activity content. Looks good so far.
ImageView image = new ImageView(this); - Creates a new ImageView. Nothing technically wrong with this but probably not what you want because...
image = (ImageView) findViewById(R.id.test_image); - Searches through the current view hierarchy (the one where you inflated R.layout.main above) for a View with the id test_image. This overwrites your image variable, throwing out the new ImageView you created on the line above to be garbage collected.
setContentView(image); - Attempts to set the view with id test_image as the root of your activity's view hierarchy, overwriting the hierarchy you inflated in your first setContentView call. I'm guessing that if you check the stack trace from the exception, your app is crashing because at this point image already has a parent view and you're trying to give it a new one without removing it from its current parent first.
Once a view has been attached to the hierarchy you do not need to re-add it when content changes. Simply change the content in-place if that's what you want to do. Calling setContentView more than once is a very unusual thing to do. In the vast majority of cases it's unnecessary.
Since your main layout already specifies the drawable you want in your ImageView your onCreate can be shortened to just the first two lines to simply show the image:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.id.main);
}
For my Android application, I want to have a view which allows a user to click a plus button to add EditText fields, and next to the EditText fields, I want to have minus buttons that will remove them from the view. In essence, something that is very similar to adding multiple phone numbers/email addresses in the edit Contact interface on Android.
I imagine I will need to do this by inflating my main view with a separate one that contains the EditText and button I want to add each time. However, I have no idea how I will manage identifying each EditText and button with a unique ID, and thus, I have no idea how I would manage to grab the values of each EditText for saving to my database. Can someone advise me on what I need to do? Thank you.
If you have inflated a sub-layout, then you should now have a View object.
You can then call findViewById(R.id.edit_text_1) on that View — assuming that you supplied IDs in the sub-layout XML.
So long as you keep track of each of the parent Views, you can always use findViewById() on them. Or after inflation, if you really want you can set a new, globally-unique ID on each EditText using setId().
You can assign a view widget dynamically, and generate your own ID as it is assigned.
Button b = new Button(this);
b.setId(myId);
Then you can refer back to that widget.
findViewById(myId);