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:59

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:

 Spinner hdSpinner = (Spinner)view.findViewById(R.id.accountsSpinner);

Then I passed the hdSpinner as a parameter during creation of the adapter object:

  mTransactionAdapter = new TransactionAdapter(getActivity(),
        R.layout.transactions_list_item, null, from, to, 0, hdSpinner);
查看更多
忆尘夕之涩
3楼-- · 2018-12-31 06:00

which returns null

Possibly because you are calling it too early. Wait until onFinishInflate(). Here is a sample project demonstrating a custom View accessing its contents.

查看更多
旧人旧事旧时光
4楼-- · 2018-12-31 06:00

Possibly, you are calling findViewById before calling setContentView? If that's the case, try calling findViewById AFTER calling setContentView

查看更多
弹指情弦暗扣
5楼-- · 2018-12-31 06:02

I have the same problem, but I think its worth sharing with you guys. If you have to findViewById in custom layout, for example:

public class MiniPlayerControllBar extends LinearLayout {
    //code
}

you cannot get the view in constructor. You should call findViewById after view has inflated. Their is a method you can override onFinishInflate

查看更多
明月照影归
6楼-- · 2018-12-31 06:03

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?

查看更多
刘海飞了
7楼-- · 2018-12-31 06:05

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.

        if (convertView == null) {
            LayoutInflater inflater =  (LayoutInflater) myContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.child_layout, null);
        }

I found this here.

查看更多
登录 后发表回答