Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

findViewById returns null on an <include> view

Tags:

People also ask

What does findViewById return?

findViewById returns an instance of View , which is then cast to the target class. All good so far. To setup the view, findViewById constructs an AttributeSet from the parameters in the associated XML declaration which it passes to the constructor of View . We then cast the View instance to Button .

What does findViewById return if it doesn't find a view object with the matching ID?

. findViewById(R. id. retry) would always return null.

What is Viewview findViewById?

view. findViewById() is used to find a view inside a specific other view. For example to find a view inside your ListView row layout. Your app crashes without it because the view you're searching for is inside rowView .

How does findViewById work on Android?

In the case of an Activity , findViewById starts the search from the content view (set with setContentView ) of the activity which is the view hierarchy inflated from the layout resource. So the View inflated from R. layout.


Let's get straight to code.

Activity Code, onCreate method

setContentView(R.layout.main);

View main_view = findViewById(R.id.main_view);
View view_flipper = main_view.findViewById(R.id.vf);
View first_tab = view_flipper.findViewById(R.id.prev_pane);

The main.xml layout code:

<ViewFlipper android:id="@+id/vf"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >  

    <include android:id="@+id/prev_pane" layout="@layout/vf_inner" /> 
<include android:id="@+id/cur_pane" layout="@layout/vf_inner" />
    <include android:id="@+id/next_pane" layout="@layout/vf_inner" />
</ViewFlipper>

The problem! Main view is correctly found, so is ViewFlipper, yet prev_pane or cur_pane or next_pane are not found, a null pointer is returned. Any ideas why?

This activity is called through a tab by using an Intent. All references are resolved correctly except id's on includes

When I inspect variables, I find the contents of vf_inner layout in mChildren