首页 文章

应用程序在锁定/睡眠屏幕崩溃 - NullPointerException

提问于
浏览
5

使用设备进行测试 - 每当我锁定屏幕(或在闲置1分钟后休眠)时,应用程序崩溃,并且 logcatonResume() 方法中始终显示 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 .

FATAL EXCEPTION:main java.lang.RuntimeException:无法在android上恢复活动{nmw.nss / nmw.nss.CriticalList}:android.app.ActivityThread.performResumeActivity(ActivityThread.java:2820)中的java.lang.NullPointerException . app.ActivityThread.handleResumeActivity(ActivityThread.java:2859)在Android.app.ActivityThread $ android.app.ActivityThread.access $ 600(ActivityThread.java:139)的android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2242) H.handleMessage(ActivityThread.java:1262)位于android.app.A.运行时,Android.A.T.Thread.main上的android.os.Handler.dispatchMessage(Handler.java:99)以及android.app.Looper.loop(Looper.java:154)(ActivityThread) .java:4974)at java.lang.reflect.Method.invokeNative(Native Method)at 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.NullP在android.app.InstrumentationCallActivityRnume(Instrumentation.java:1236)的android.app.Activity.performResume(Activity.java:4620)上的nmw.nss.CriticalList.onResume(CriticalList.java:93)的ointerException .ActivityThread.performResumeActivity(ActivityThread.java:2804)... 12个其他

谢谢 .

编辑:我添加了完整的活动(排序), xmllogcat . 我是否需要覆盖任何 Activity 方法,也许 onStoponPause 才能使其工作?

2 回答

  • 4

    终于找到了!我的应用程序基本上是设计为在横向模式下运行,并且在清单中我添加了------- android:screenOrientation =“landscape” .

    好吧,这就是问题:在你锁定设备的模式下,findViewById()总是在纵向模式下搜索XML并且在我的情况下,根本没有ImageView . 因此NullPointerException .

  • 0

    我假设您之前没有调用 setContentView() ,因此 Imageview imageAddToMultiple 为null,当您尝试设置侦听器时,它会崩溃 .

    希望能帮助到你!

相关问题