java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.gchat / com.example.gchat.HomeActivity}:java.lang.NullPointerException

问题描述 投票:0回答:1

我正在尝试创建这样的菜单(灰色背景的菜单)-

<< img src =“ https://image.soinside.com/eyJ1cmwiOiAiaHR0cHM6Ly9pLnN0YWNrLmltZ3VyLmNvbS95VGVEdS5wbmcifQ==” alt =“在此处输入图像描述”>

我使用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代码-

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

当我运行代码时,出现了此问题标题中的异常。 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

我在这里和Google进行了很多搜索,但找不到任何有用的信息。我该如何纠正此错误?

android textview onclicklistener
1个回答
2
投票

您甚至在扩大版面之前就访问子视图,这是获得NullPointerException的首要原因>

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

首先膨胀布局,然后使用该膨胀的布局引用访问其子视图。否则,系统将无法理解您尝试使用的视图是否属于膨胀布局。

例如:

View view = getLayoutInflater().inflate(R.layout.menu_layout, null);

TextView tv1 = (TextView) view.findViewById(R.id.textViewMenu1);

更新:

您应按如下所示初始化充气式布局的TextViews

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);
}
© www.soinside.com 2019 - 2024. All rights reserved.