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.
findViewById also can return null if you're inside a Fragment. As described here: findViewById in Fragment
You should call getView() to return the top level View inside a Fragment. Then you can find the layout items (buttons, textviews, etc)
In my case, I had 2 activites in my project,
main.xml
andmain2.xml
. From the beginning,main2
was a copy ofmain
, and everything worked well, until I added newTextView
tomain2
, so theR.id.textview1
became available for the rest of app. Then I tried to fetch it by standard calling:and it was always null. It turned out, that in
onCreate
constructor I was instantiating notmain2
, but the other one. I had:instead of
I noticed this after I arrived here, on the site.
In my experience, it seems that this can also happen when your code is called after OnDestroyView (when the fragment is on the back stack.) If you are updating the UI on input from a BroadCastReceiver, you ought to check if this is the case.
Alongside the classic causes, mentioned elsewhere:
setContentView()
beforefindViewById()
id
you want is in the view or layout you've given tosetContentView()
id
isn't accidentally duplicated in different layoutsThere is one I have found for custom views in standard layouts, which goes against the documentation:
In theory you can create a custom view and add it to a layout (see here). However, I have found that in such situations, sometimes the
id
attribute works for all the views in the layout except the custom ones. The solution I use is:FrameLayout
with the same layout properties as you would like the custom view to have. Give it an appropriateid
, sayframe_for_custom_view
.In
onCreate
:which puts the custom view in the frame.
I'm pretty new to Android/Eclipse, by mistake I added the UI stuff to
activity_main.xml
instead offragment_main.xml
. Took me some hours to figure that out...