Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FindViewByID returns null for CustomView

Tags:

android

null

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.layout.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>
like image 673
Franziskus Karsunke Avatar asked Dec 21 '22 12:12

Franziskus Karsunke


1 Answers

Add this constructor to your custom view class..

public ListWithCompassView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
like image 95
Sujit Avatar answered Jan 01 '23 12:01

Sujit