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

前端之家收集整理的这篇文章主要介绍了android – 应用程序在锁定/睡眠屏幕崩溃 – NullPointerException前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

使用设备进行测试 – 每当我锁定屏幕(或闲置1分钟后休眠),应用程序崩溃,logcat会在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:

我不知道为什么我会得到这个NullPointer.

FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to resume activity {nmw.nss/nmw.nss.CriticalList}: java.lang.NullPointerException at
android.app.ActivityThread.performResumeActivity(ActivityThread.java:2820)
at
android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2859)
at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2242)
at android.app.ActivityThread.access$600(ActivityThread.java:139) at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1262)
at android.os.Handler.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) 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) Caused by:
java.lang.NullPointerException at
nmw.nss.CriticalList.onResume(CriticalList.java:93)

at
android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1236)
at android.app.Activity.performResume(Activity.java:4620) at
android.app.ActivityThread.performResumeActivity(ActivityThread.java:2804)
… 12 more

谢谢.

编辑:我添加了完整的活动(类型),xml和logcat.
我是否需要覆盖任何Activity方法,可能是onStop或onPause以使其工作?

最佳答案
终于找到了!
我的应用程序基本上是在横向模式下运行,而在清单中我添加了——-
机器人:screenOrientation = “风景”.

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

猜你在找的Android相关文章