First of all: yes, I read all the other threads on this topic. And not only those from this site... (you see, I'm a little frustrated)
Most of them come with the advice to use android:id
instead of just id
in the XML file. I did.
From others, I learned, that View.findViewById
works different than Activity.findViewById
. I handled that, too.
In my location_layout.xml
, I use:
<FrameLayout .... >
<some.package.MyCustomView ... />
<LinearLayout ... >
<TextView ...
android:id="@+id/txtLat" />
...
</LinearLayout>
</FrameLayout>
In my Activity I do:
...
setContentView( R.layout.location_layout );
and in my custom view class:
...
TextView tv = (TextView) findViewById( R.id.txtLat );
which returns null
. Doing this, my Activity works fine. So maybe it's because of the Activity.findViewById
and View.findViewById
differences. So I stored the context passed to the customs view constructor locally and tried:
...
TextView tv = (TextView) ((Activity) context).findViewById( R.id.txtLat );
which also returned null
.
Then, I changed my custom view to extend ViewGroup
instead View
and changed the location_layout.xml
to let the TextView
be a direct child of my custom view, so that the View.findViewById
should work as supposed. Suprise: it didn't solve anything.
So what the heck am I doing wrong?
I'll appreciate any comments.
In my case, findViewById returned null when I moved the call from a parent object into an adapter object instantiated by the parent. After trying tricks listed here without success, I moved the findViewById back into the parent object and passed the result as a parameter during instantiation of the adapter object. For example, I did this in parent object:
Then I passed the hdSpinner as a parameter during creation of the adapter object:
Possibly because you are calling it too early. Wait until
onFinishInflate()
. Here is a sample project demonstrating a customView
accessing its contents.Possibly, you are calling
findViewById
before callingsetContentView
? If that's the case, try callingfindViewById
AFTER callingsetContentView
I have the same problem, but I think its worth sharing with you guys. If you have to findViewById in custom layout, for example:
you cannot get the view in constructor. You should call findViewById after view has inflated. Their is a method you can override
onFinishInflate
For me I had two xml layouts for the same activity - one in portrait mode and one in landscape. Of course I had changed the id of an object in the landscape xml but had forgotten to make the same change in the portrait version. Make sure if you change one you do the same to the other xml or you will not get an error until you run/debug it and it can't find the id you didn't change. Oh dumb mistakes, why must you punish me so?
A answer for those using ExpandableListView and run into this question based on it's title.
I had this error attempting to work with TextViews in my child and group views as part of an ExpandableListView implementation.
You can use something like the following in your implementations of the getChildView() and getGroupView() methods.
I found this here.