rpurohit
rpurohit

Reputation: 364

findViewById returning null on LinearLayout

I am programming an app which has many views, and then based on which view you click, it will update the views. When I try to find the layout to update the views, findViewById returns null. FYI: I am calling findViewById from a function that onCreate() calls. And yes, I am calling setContentView before calling that function. MainActivity.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    updateViews();

}


public void updateViews(){
     LinearLayout layout = (LinearLayout) findViewById(R.id.list_layout);
    layout.removeAllViews(); //NPE: Attempt to invoke virtual method 'android.widget.LinearLayout.removeAllViews()' on a null object reference
}

What can I do to fix this issue? I suspect it has something to do with layout inflation, but I am not sure.

Upvotes: 0

Views: 1627

Answers (2)

shayan
shayan

Reputation: 220

When you use layout.removeAllViews(). that means remove all child into the layout and dose not contain parent layout.

If you have this code:

  <Linearlayout
        android:layout_width="match_parent"
        android:layout_height="match_parent" 
        android:id="@+id/layout"
        android:background="@android:color/holo_blue_dark">

        <ToggleButton
            android:id="@+id/toggleButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="ToggleButton" />

        <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Large Text"
            android:textAppearance="?android:attr/textAppearanceLarge" />

    </Linearlayout>

and call layout.removeAllViews() method. this remove textView and toggleButton but dose not remove parent layout(layout id).

If you wantremove child and parent view . You can use like this:

 ((ViewGroup)layout.getParent()).removeAllViews();

Upvotes: 0

piotrek1543
piotrek1543

Reputation: 19361

Personally, I see only two reasons of NPE (Null Pointer Exception):

First. You don't have already LinearLayout with R.id.list_layout which is a really silly, because your project is making and it returns NPE.

You said that you have that view with this android:id. Let's say you have a TextView with this id. I swear that in that case Android Studio won't say about NPE, but about wrong view attributes like:

Hey rpurohit! There's no TextView matched with this id. Check please correctness of your configuration.

Second. You said also that you have only one view, which is this LinearLayout. I can say only

Congratulations! You've found an issue!

and explain you that you're trying to remove all views from your master ViewGroup, but like you said:

The LinearLayout is the only thing that is inside the XML file.

so your IDE tries to remove any view from its parent, but as there's no ChildViews, it tells you about Null Pointer Exception.

I'm sure that now you understand your simple mistake :-)

Upvotes: 1

Related Questions