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 particular case, I was trying to add a footer to a ListView. The following call in onCreate() was returning null.
Changing this to inflate the footer view instead of finding it by ID solved this issue.
In addition of the above solutions you make sure the
tools:context=".TakeMultipleImages"
in the layout is same value in the mainfest.xml file :android:name=".TakeMultipleImages"
for the same activity element. it is occur when use copy and paste to create new activityI've tried all of the above nothing was working.. so I had to make my ImageView static
public static ImageView texture;
and thentexture = (ImageView) findViewById(R.id.texture_back);
, I don't think it's a good approach though but this really worked for my case :)FindViewById can be null if you call the wrong super constructor in a custom view. The ID tag is part of attrs, so if you ignore attrs, you delete the ID.
This would be wrong
This is correct
Set the activity content from a layout resource. ie.,
setContentView(R.layout.basicXml)
;I had this same problem. I was using a third-party library that allows you to override their adapter for a GridView and to specify your own layout for each GridView cell.
I finally realized what was happening. Eclipse was still using the library's layout xml file for each cell in the GridView, even though it gave no indication of this. In my custom adapter, it indicated that it was using the xml resource from my own project even though at runtime, it wasn't.
So what I did was to make sure my custom xml layouts and ids were different from those still sitting in the library, cleaned the project and then it started reading the correct custom layouts that were in my project.
In short, be careful if you're overriding a third-party library's adapter and specifying your own layout xml for the adapter to use. If your layout inside your project has the same file name as that in the library, you might encounter a really difficult-to-find bug!