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

时间:2014-09-18 16:59:15

标签: android textview onclicklistener

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

enter image description here

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

我在这里和谷歌搜索了很多,但是找不到任何有用的东西。如何更正此错误?

1 个答案:

答案 0 :(得分: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);
}