. lang。运行时异常:无法启动activity ComponentInfo{com.example.gchat/com.example.gchat。HomeActivity }:java.lang.NullPointerException

时间:2023-01-12 20:50:18

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

我试着创建一个这样的菜单(灰色背景的菜单)-

. lang。运行时异常:无法启动activity ComponentInfo{com.example.gchat/com.example.gchat。HomeActivity }:java.lang.NullPointerException

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

我用的是LayoutInflator。下面——它的代码

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-

出来。menu_layout代码-

<?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-

当我运行我的代码时,我得到了这个问题标题中的异常。LogCat详细信息如下

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 ?

我在谷歌上搜索了很多,但是没有发现任何有用的东西。我怎样才能改正这个错误呢?

1 个解决方案

#1


2  

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

您甚至在扩展布局之前就访问了子视图,这是获得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

您应该初始化膨胀布局的textview,如下所示

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);
}

#1


2  

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

您甚至在扩展布局之前就访问了子视图,这是获得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

您应该初始化膨胀布局的textview,如下所示

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);
}