-2

I'm developing an Android application but I've encountered a problem when dealing with ViewStub. In particular in my imus.java class I have this code in the onCreate method:

    Stub = (ViewStub) findViewById(R.id.imu_toinclude);
    inflated_initialization = Stub.inflate();

In the corresponding imus.xml I have:

            <ViewStub
            android:id="@+id/imu_toinclude"
            android:layout="@layout/initialization_imu"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inflatedId="@+id/initialization_2" />

where initialization_imu is the name of the layout I would like ti inflate. The error I get is:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.ViewStub.inflate()' on a null object reference

Hope that someone can help me

  • Cause object you are trying to access it, is null , https://docs.oracle.com/javase/7/docs/api/java/lang/NullPointerException.html – Lunatic Jun 03 '22 at 12:50
  • Thank you for the answer. The problem is that I don't understand why the object is null – Engineer78 Jun 03 '22 at 12:55

0 Answers0