Search code examples
androidtextviewonclicklistener

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.gchat/com.example.gchat.HomeActivity}: java.lang.NullPointerException


I was trying to create a menu like this(Menu in gray background)-

enter image description here

I made it using a LayoutInflator. Its code is below-

ImageView imgv1, imgv2;
TextView tv1, tv2, tv3;
private Animation down;
View child;

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);

    imgv1 = (ImageView) findViewById(R.id.imageViewHA1);
    imgv2 = (ImageView) findViewById(R.id.imageViewHA2);

    tv1 = (TextView) findViewById(R.id.textViewMenu1);
    tv2 = (TextView) findViewById(R.id.textViewMenu2);
    tv3 = (TextView) findViewById(R.id.textViewMenu3);

    down = AnimationUtils.loadAnimation(this, R.anim.slide_down);

    imgv2.setOnClickListener(new OnClickListener()
    {

        @Override
        public void onClick(View v)
        {
            LinearLayout item = (LinearLayout) findViewById(R.id.linearLayout2);
            if (child == null)
            {
                item.setAnimation(down);
                down.start();
                child = getLayoutInflater().inflate(R.layout.menu_layout, null);
                item.addView(child);
            } else
            {
                item.removeAllViews();
                child = null;
            }
        }
    });

    tv3.setOnClickListener(new OnClickListener()
    {

        @Override
        public void onClick(View v)
        {
            System.exit(0);
        }
    });
}

R.layout.menu_layout code-

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >

<LinearLayout
    android:id="@+id/menulayout1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@color/White"
    android:orientation="vertical"
    android:showDividers="middle" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="3dp"
        android:layout_marginRight="3dp"
        android:layout_marginTop="2dp"
        android:background="@color/DimGray" >

        <TextView
            android:id="@+id/textViewMenu1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:paddingBottom="3dp"
            android:paddingLeft="10dp"
            android:paddingRight="10dp"
            android:paddingTop="3dp"
            android:text="New Group"
            android:textColor="@color/White"
            android:textStyle="bold" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="3dp"
        android:layout_marginRight="3dp"
        android:layout_marginTop="2dp"
        android:background="@color/DimGray" >

        <TextView
            android:id="@+id/textViewMenu2"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:paddingBottom="3dp"
            android:paddingLeft="10dp"
            android:paddingRight="10dp"
            android:paddingTop="3dp"
            android:text="Contacts"
            android:textColor="@color/White"
            android:textStyle="bold" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="3dp"
        android:layout_marginRight="3dp"
        android:layout_marginTop="2dp"
        android:background="@color/DimGray"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/textViewMenu3"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:paddingBottom="3dp"
            android:paddingLeft="10dp"
            android:paddingRight="10dp"
            android:paddingTop="3dp"
            android:text="Exit"
            android:textColor="@color/White"
            android:textStyle="bold" />

    </LinearLayout>

</LinearLayout>

</RelativeLayout>

When I ran my code I got the exception which is in the title of this question. The LogCat details are here-

09-18 22:06:50.369: E/AndroidRuntime(952): FATAL EXCEPTION: main
09-18 22:06:50.369: E/AndroidRuntime(952): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.gchat/com.example.gchat.HomeActivity}: java.lang.NullPointerException
09-18 22:06:50.369: E/AndroidRuntime(952):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
09-18 22:06:50.369: E/AndroidRuntime(952):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
09-18 22:06:50.369: E/AndroidRuntime(952):  at android.app.ActivityThread.access$600(ActivityThread.java:130)
09-18 22:06:50.369: E/AndroidRuntime(952):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
09-18 22:06:50.369: E/AndroidRuntime(952):  at android.os.Handler.dispatchMessage(Handler.java:99)
09-18 22:06:50.369: E/AndroidRuntime(952):  at android.os.Looper.loop(Looper.java:137)
09-18 22:06:50.369: E/AndroidRuntime(952):  at android.app.ActivityThread.main(ActivityThread.java:4745)
09-18 22:06:50.369: E/AndroidRuntime(952):  at java.lang.reflect.Method.invokeNative(Native Method)
09-18 22:06:50.369: E/AndroidRuntime(952):  at java.lang.reflect.Method.invoke(Method.java:511)
09-18 22:06:50.369: E/AndroidRuntime(952):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
09-18 22:06:50.369: E/AndroidRuntime(952):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-18 22:06:50.369: E/AndroidRuntime(952):  at dalvik.system.NativeStart.main(Native Method)
09-18 22:06:50.369: E/AndroidRuntime(952): Caused by: java.lang.NullPointerException
09-18 22:06:50.369: E/AndroidRuntime(952):  at com.example.gchat.HomeActivity.onCreate(HomeActivity.java:58)
09-18 22:06:50.369: E/AndroidRuntime(952):  at android.app.Activity.performCreate(Activity.java:5008)
09-18 22:06:50.369: E/AndroidRuntime(952):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
09-18 22:06:50.369: E/AndroidRuntime(952):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
09-18 22:06:50.369: E/AndroidRuntime(952):  ... 11 more
09-18 22:16:03.709: E/Trace(998): error opening trace file: No such file or directory (2)
09-18 22:16:04.238: D/AndroidRuntime(998): Shutting down VM
09-18 22:16:04.238: W/dalvikvm(998): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
09-18 22:16:04.248: E/AndroidRuntime(998): FATAL EXCEPTION: main
09-18 22:16:04.248: E/AndroidRuntime(998): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.gchat/com.example.gchat.HomeActivity}: java.lang.ClassCastException: com.example.gchat.HomeActivity cannot be cast to android.view.View$OnClickListener
09-18 22:16:04.248: E/AndroidRuntime(998):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
09-18 22:16:04.248: E/AndroidRuntime(998):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
09-18 22:16:04.248: E/AndroidRuntime(998):  at android.app.ActivityThread.access$600(ActivityThread.java:130)
09-18 22:16:04.248: E/AndroidRuntime(998):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
09-18 22:16:04.248: E/AndroidRuntime(998):  at android.os.Handler.dispatchMessage(Handler.java:99)
09-18 22:16:04.248: E/AndroidRuntime(998):  at android.os.Looper.loop(Looper.java:137)
09-18 22:16:04.248: E/AndroidRuntime(998):  at android.app.ActivityThread.main(ActivityThread.java:4745)
09-18 22:16:04.248: E/AndroidRuntime(998):  at java.lang.reflect.Method.invokeNative(Native Method)
09-18 22:16:04.248: E/AndroidRuntime(998):  at java.lang.reflect.Method.invoke(Method.java:511)
09-18 22:16:04.248: E/AndroidRuntime(998):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
09-18 22:16:04.248: E/AndroidRuntime(998):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-18 22:16:04.248: E/AndroidRuntime(998):  at dalvik.system.NativeStart.main(Native Method)
09-18 22:16:04.248: E/AndroidRuntime(998): Caused by: java.lang.ClassCastException: com.example.gchat.HomeActivity cannot be cast to android.view.View$OnClickListener
09-18 22:16:04.248: E/AndroidRuntime(998):  at com.example.gchat.HomeActivity.onCreate(HomeActivity.java:35)
09-18 22:16:04.248: E/AndroidRuntime(998):  at android.app.Activity.performCreate(Activity.java:5008)
09-18 22:16:04.248: E/AndroidRuntime(998):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
09-18 22:16:04.248: E/AndroidRuntime(998):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
09-18 22:16:04.248: E/AndroidRuntime(998):  ... 11 more
09-18 22:20:33.669: E/Trace(1046): error opening trace file: No such file or directory (2)
09-18 22:20:33.989: D/AndroidRuntime(1046): Shutting down VM
09-18 22:20:34.029: W/dalvikvm(1046): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
09-18 22:20:34.079: E/AndroidRuntime(1046): FATAL EXCEPTION: main
09-18 22:20:34.079: E/AndroidRuntime(1046): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.gchat/com.example.gchat.HomeActivity}: java.lang.NullPointerException
09-18 22:20:34.079: E/AndroidRuntime(1046):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
09-18 22:20:34.079: E/AndroidRuntime(1046):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
09-18 22:20:34.079: E/AndroidRuntime(1046):     at android.app.ActivityThread.access$600(ActivityThread.java:130)
09-18 22:20:34.079: E/AndroidRuntime(1046):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
09-18 22:20:34.079: E/AndroidRuntime(1046):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-18 22:20:34.079: E/AndroidRuntime(1046):     at android.os.Looper.loop(Looper.java:137)
09-18 22:20:34.079: E/AndroidRuntime(1046):     at android.app.ActivityThread.main(ActivityThread.java:4745)
09-18 22:20:34.079: E/AndroidRuntime(1046):     at java.lang.reflect.Method.invokeNative(Native Method)
09-18 22:20:34.079: E/AndroidRuntime(1046):     at java.lang.reflect.Method.invoke(Method.java:511)
09-18 22:20:34.079: E/AndroidRuntime(1046):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
09-18 22:20:34.079: E/AndroidRuntime(1046):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-18 22:20:34.079: E/AndroidRuntime(1046):     at dalvik.system.NativeStart.main(Native Method)
09-18 22:20:34.079: E/AndroidRuntime(1046): Caused by: java.lang.NullPointerException
09-18 22:20:34.079: E/AndroidRuntime(1046):     at com.example.gchat.HomeActivity.onCreate(HomeActivity.java:58)
09-18 22:20:34.079: E/AndroidRuntime(1046):     at android.app.Activity.performCreate(Activity.java:5008)
09-18 22:20:34.079: E/AndroidRuntime(1046):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
09-18 22:20:34.079: E/AndroidRuntime(1046):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
09-18 22:20:34.079: E/AndroidRuntime(1046):     ... 11 more

I searched a lot here and in Google, but couldn't found anything helpful. How can I correct this error ?


Solution

  • You are accessing child views even before inflating the layout and that's the first cause you are getting NullPointerException

    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.gchat/com.example.gchat.HomeActivity}: java.lang.NullPointerException
    

    First inflate the layout then access its child views using that inflated layout reference. Otherwise, system will not understand that the view, you are trying use, is belongs to inflated layout.

    As example:

    View view = getLayoutInflater().inflate(R.layout.menu_layout, null);
    
    TextView tv1 = (TextView) view.findViewById(R.id.textViewMenu1);
    

    Update:

    you should initialize your TextViews of inflated layout as below

    if (child == null) {
        item.setAnimation(down);
        down.start();
    
        child = getLayoutInflater().inflate(R.layout.menu_layout, null);
        tv1 = (TextView) child.findViewById(R.id.textViewMenu1);
        tv2 = (TextView) child.findViewById(R.id.textViewMenu2);
        tv3 = (TextView) child.findViewById(R.id.textViewMenu3);
    
        tv3.setOnClickListener(new OnClickListener()
        {
    
            @Override
            public void onClick(View v)
            {
                //you code
            }
        });
    
        item.addView(child);
    }