开发者

FindViewByID returns null for CustomView

开发者 https://www.devze.com 2023-03-13 11:41 出处:网络
I am having trouble with findViewById again in android. My Activity never exits the while loop, because the listview is always null. I already tried rebuilding. I know about the onFinishedInflating me

I am having trouble with findViewById again in android. My Activity never exits the while loop, because the listview is always null. I already tried rebuilding. I know about the onFinishedInflating method for views, but it doesn't help me in my Activity.

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.setContentView(R.l开发者_运维百科ayout.list_with_compass);

    ListWithCompassView lv = (ListWithCompassView) this.findViewById(R.id.compass_list);

    while(lv == null) {
        Log.d("waiting", "waiting1");
        lv = (ListWithCompassView) this.findViewById(R.id.compass_list);
    }
    this.setListView();
}

This is the XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/linear_layout_compass" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android">
    <karsunke.view.ListWithCompassView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:id="@+id/compass_list">
    </karsunke.view.ListWithCompassView>
</LinearLayout>


Add this constructor to your custom view class..

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


It's a long shoot but try cleaning your project and rebuilding it. Sometimes I received null in findviewbyid for no reason until i rebuilt my project. If that doen't help post the custom class so we can see if the problem is there.

0

精彩评论

暂无评论...
验证码 换一张
取 消