findViewByID returns null

2018-12-31 05:43发布

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.

27条回答
呛了眼睛熬了心
2楼-- · 2018-12-31 05:43

In my particular case, I was trying to add a footer to a ListView. The following call in onCreate() was returning null.

TextView footerView = (TextView) placesListView.findViewById(R.id.footer);

Changing this to inflate the footer view instead of finding it by ID solved this issue.

View footerView = ((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.footer_view, null, false);
查看更多
心情的温度
3楼-- · 2018-12-31 05:45

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 activity

查看更多
忆尘夕之涩
4楼-- · 2018-12-31 05:46

I've tried all of the above nothing was working.. so I had to make my ImageView static public static ImageView texture; and then texture = (ImageView) findViewById(R.id.texture_back); , I don't think it's a good approach though but this really worked for my case :)

查看更多
看风景的人
5楼-- · 2018-12-31 05:47

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

public CameraSurfaceView(Context context, AttributeSet attrs) {
        super(context);
}

This is correct

public CameraSurfaceView(Context context, AttributeSet attrs) {
        super(context,attrs);
}
查看更多
爱死公子算了
6楼-- · 2018-12-31 05:48

Set the activity content from a layout resource. ie.,setContentView(R.layout.basicXml);

查看更多
孤独总比滥情好
7楼-- · 2018-12-31 05:48

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!

查看更多
登录 后发表回答