【问题标题】:App crashes on lock/sleep screen - NullPointerException应用程序在锁定/睡眠屏幕上崩溃 - NullPointerException
【发布时间】:2012-06-16 07:46:18
【问题描述】:

使用设备进行测试 - 每当我锁定屏幕(或在空闲 1 分钟后进入睡眠状态)时,应用程序就会崩溃,并且 logcatonResume() 方法中始终显示 NullPointerException 上的 imageView.setOnClickListener

这是我得到异常的一小部分代码:

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 at 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) 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1262) 在 android.os.Handler.dispatchMessage(Handler.java:99) 在 android.os.Looper.loop(Looper.java:154) 在 android.app.ActivityThread.main(ActivityThread.java:4974) 在 java.lang.reflect.Method.invokeNative(Native Method) 在 java.lang.reflect.Method.invoke(Method.java:511) 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 在 dalvik.system.NativeStart.main(Native Method) 原因: java.lang.NullPointerException 在 nmw.nss.CriticalList.onResume(CriticalList.java:93)

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

谢谢。

编辑:我添加了完整的活动(有点)、xmllogcat。 我是否需要重写任何 Activity 方法,也许是 onStoponPause 才能完成这项工作?

【问题讨论】:

  • 但是你在哪里声明了 listview ?
  • @gtumca-MAC listview 无关紧要,这只是我活动的一部分。为了确认这一点,我完全删除了 imageView 及其监听器代码,它运行良好。所以肯定 findViewById 返回 null

标签: android nullpointerexception


【解决方案1】:

终于找到了! 我的应用程序基本上设计为在横向模式下运行,并且在清单中我添加了-------- 安卓:screenOrientation="风景"。

好吧,问题来了:锁定设备的模式无关紧要,findViewById() 总是以纵向模式搜索 XML,而在我的情况下,它根本没有 ImageView。因此出现 NullPointerException。

【讨论】:

  • 我的应用程序在这里使用相同的 prblm 你能告诉我解决方案是什么
  • 由于我从未使用过纵向模式,所以布局 XML 包含什么并不重要,我只是将横向的所有代码从 XML 复制到纵向
  • 这就是问题所在,是的。以下是解决方法:stackoverflow.com/a/29494115/1760313
【解决方案2】:

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

希望对你有帮助!

【讨论】:

  • 我在 onCreate 方法中调用了它,我尝试将它放在 onResume 中,它没有任何区别
  • 你定义了 listenerAddToMultipleItems 吗?
  • 是的,当然......它所做的只是为列表设置不同类型的适配器(与问题无关,因此我没有提出)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-24
  • 2011-11-20
  • 2015-05-21
  • 2012-06-08
  • 1970-01-01
  • 2017-05-08
相关资源
最近更新 更多