2012-06-16 40 views
4

使用用於測試的裝置 - 每當我鎖定屏幕(或1分鐘後空閒睡),應用程序崩潰和logcat示出了NullPointerException總是在onResume()方法的imageView.setOnClickListener應用崩潰鎖定/睡眠屏幕上 - 的NullPointerException

下面的代碼的一小部分,我得到異常:

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.critical_items_list); 

    // instantiate member variables 

    mExtras = getIntent().getExtras(); 
    mItems_P1 = mExtras.getParcelableArrayList(EXTRA_ITEMS_P1); 
    mItems_P2 = mExtras.getParcelableArrayList(EXTRA_ITEMS_P2); 
    mItems_Engine = mExtras.getParcelableArrayList(EXTRA_ITEMS_ENGINE); 
} 

protected void onResume() { 
    super.onResume(); 

    ImageView imageAddToMultiple = (ImageView) findViewById(R.id.image_add_to_multiple); 

    //This is where the I get the NullPointer 
    imageAddToMultiple.setOnClickListener(listenerAddToMultipleItems); 

    refreshLists(); 
    ListView listP1 = (ListView) findViewById(R.id.list_p1); 
    ListView listP2 = (ListView) findViewById(R.id.list_p2); 
    ListView listEngine = (ListView) findViewById(R.id.list_engine); 

    listP1.setAdapter(new MyAdapter(mList_P1)); 
    listP2.setAdapter(new MyAdapter(mList_P2)); 
    listEngine.setAdapter(new MyAdapter(mList_Engine)); 
} 

的ImageView的本身就在於一個RelativeLayout

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical" > 

<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="@dimen/standard_bar_height" 
    android:background="@color/green" 
    android:baselineAligned="false" > 

    <LinearLayout 
     android:layout_width="0dp" 
     android:layout_height="fill_parent" 
     android:layout_weight="1" 
     android:gravity="center" > 

     <TextView 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:gravity="center" 
      android:text="@string/plant_1" 
      android:textColor="@android:color/black" /> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="0dp" 
     android:layout_height="fill_parent" 
     android:layout_weight="1" 
     android:gravity="center" > 

     <TextView 
      android:layout_width="fill_parent" 
      android:layout_height="match_parent" 
      android:layout_gravity="center" 
      android:gravity="center" 
      android:text="@string/plant_2" 
      android:textColor="@android:color/black" /> 
    </LinearLayout> 

    <RelativeLayout 
     android:id="@+id/RelativeLayout1" 
     android:layout_width="0dp" 
     android:layout_height="fill_parent" 
     android:layout_weight="1" > 

     <ImageView 
      android:id="@+id/image_add_to_multiple" 
      android:layout_width="@dimen/standard_bar_height" 
      android:layout_height="@dimen/standard_bar_height" 
      android:layout_alignParentRight="true" 
      android:layout_centerHorizontal="true" 
      android:contentDescription="@string/description_add_to_multiple_items" 
      android:src="@android:drawable/ic_menu_add" /> 

     <TextView 
      android:id="@+id/TextView1" 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:layout_centerInParent="true" 
      android:gravity="center" 
      android:text="@string/engine" 
      android:textColor="@android:color/black" /> 
    </RelativeLayout> 
</LinearLayout> 

<LinearLayout 
    android:id="@+id/linearLayout1" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="horizontal" > 

     <ListView 
      android:id="@+id/list_p1" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" > 
     </ListView> 

     <View 
      android:layout_width="2dp" 
      android:layout_height="fill_parent" 
      android:background="@color/green" /> 

     <ListView 
      android:id="@+id/list_p2" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" > 
     </ListView> 

     <View 
      android:layout_width="2dp" 
      android:layout_height="fill_parent" 
      android:background="@color/green" /> 

     <ListView 
      android:id="@+id/list_engine" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" > 
     </ListView> 
    </LinearLayout> 
</LinearLayout> 

我不知道爲什麼我會得到這NullPointer

致命異常:主 了java.lang.RuntimeException:無法恢復活動{nmw.nss/nmw.nss.CriticalList}:顯示java.lang.NullPointerException在 android.app.ActivityThread.performResumeActivity(ActivityThread。 Java的:2820) 在 android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2859) 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2242) 在android.app.ActivityThread.access $ 600(ActivityThread .java:139)at android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1262) at android.os.Hand ler.dispatchMessage(Handler.java:99)at android.os.Looper.loop(Looper.java:154)at android.app.ActivityThread.main(ActivityThread.java:4974)at java.lang.reflect。 Method.invokeNative(Native Method) java.lang.reflect.Method.invoke(Method.java:511)at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:784) at com .android.internal.os.ZygoteInit.main(ZygoteInit.java:551)at dalvik.system.NativeStart.main(Native Method)引起: java.lang.NullPointerException在 nmw.nss.CriticalList.onResume(CriticalList .java:93)

at android.a pp.Instrumentation.callActivityOnResume(Instrumentation.java:1236) 在android.app.Activity.performResume(Activity.java:4620)在 android.app.ActivityThread.performResumeActivity(ActivityThread.java:2804) ... 12更多

謝謝。

編輯:我已添加完整的活動(排序),xmllogcat。 我是否需要重寫Activity方法中的任何一種,或許要使用onStop或來使其工作?

+1

粘貼logcat在這裏.. !!! –

+0

但你在哪裏聲明瞭listview? – MAC

+1

發送您的全部代碼 – user1208720

回答

0

我假設你之前沒有調用過setContentView(),因此Imageview imageAddToMultiple爲空,當你試圖設置偵聽器時,它崩潰了。

希望它有幫助!

+0

我已經在onCreate方法中調用它,我試圖把它放在onResume中,並且它沒有區別 – Arjun

+0

您是否定義了listenerAddToMultipleItems? –

+0

是的,當然......它所做的就是爲列表設置一種不同類型的適配器(與問題無關,因此我沒有放棄) – Arjun

4

終於找到了! 我的應用程序基本上設計爲在橫向模式下運行,並在清單中添加了------- android:screenOrientation =「landscape」。這個問題:在什麼模式下你鎖定設備並不重要,findViewById()總是在肖像模式下搜索XML,而在我的情況下,根本沒有ImageView。因此NullPointerException。

+0

同樣的prblm在這裏爲我的應用程序你能告訴我什麼是這個解決方案 – Manju

+4

因爲我從來沒有使用過肖像模式,所以XML格式包含的佈局並不重要,我只是將所有的代碼從XML格式複製到肖像 – Arjun

+0

這就是問題所在。以下是解決方法:http://stackoverflow.com/a/29494115/1760313 – Tom