2014-09-18 60 views
0

我試圖創建一個像這樣的菜單(Menu in gray background) -java.lang.RuntimeException:無法啓動活動ComponentInfo {com.example.gchat/com.example.gchat.HomeActivity}:java.lang.NullPointerException

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 

我在這裏和谷歌搜索了很多,但沒有找到有用的東西。我該如何糾正這個錯誤?

+2

請發表您的完整的logcat ... – Lal 2014-09-18 16:59:58

+2

,請,擺脫System.exit – Blackbelt 2014-09-18 17:00:42

+0

@blackbelt我應該用什麼來代替System.exit? – 2014-09-18 17:05:47

回答

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

我按照你的說法試過,但我仍然得到相同的異常。你能否在我的java代碼中做一些編輯並顯示給我? – 2014-09-18 17:17:24

+0

你能告訴我,哪個視圖與id'linearLayout2'? – 2014-09-18 17:21:40

+0

這是我加入充氣機的觀點。 – 2014-09-18 17:25:14

相關問題