java 为什么退出我的活动时会崩溃?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1376783/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 16:20:04  来源:igfitidea点击:

Why am I getting a crash when exiting my Activity?

javaandroidcrashandroid-activitynullpointerexception

提问by Jeremy Logan

I am experiencing a crash in my app when I quit (via the back button) out of my Activity. So far as I can tell this is happening in the Android codebase and not mine, but I'm not completely convinced of that.

当我退出(通过后退按钮)我的 Activity 时,我的应用程序崩溃了。据我所知,这发生在 Android 代码库中而不是我的代码库中,但我并不完全相信这一点。

Here's the stacktrace from adb:

这是来自 adb 的堆栈跟踪:

AndroidRuntime  E  Uncaught handler: thread main exiting due to uncaught exception
AndroidRuntime  E  java.lang.RuntimeException: Unable to stop activity {MyApp/MyApp.MainActivity}: java.lang.NullPointerException
AndroidRuntime  E      at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3097)
AndroidRuntime  E      at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:3159)
AndroidRuntime  E      at android.app.ActivityThread.access00(ActivityThread.java:112)
AndroidRuntime  E      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1724)
AndroidRuntime  E      at android.os.Handler.dispatchMessage(Handler.java:99)
AndroidRuntime  E      at android.os.Looper.loop(Looper.java:123)
AndroidRuntime  E      at android.app.ActivityThread.main(ActivityThread.java:3948)
AndroidRuntime  E      at java.lang.reflect.Method.invokeNative(Native Method)
AndroidRuntime  E      at java.lang.reflect.Method.invoke(Method.java:521)
AndroidRuntime  E      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:782)
AndroidRuntime  E      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:540)
AndroidRuntime  E      at dalvik.system.NativeStart.main(Native Method)
AndroidRuntime  E  Caused by: java.lang.NullPointerException
AndroidRuntime  E      at android.app.Activity.performStop(Activity.java:3575)
AndroidRuntime  E      at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3092)
AndroidRuntime  E      ... 11 more

Anyone have any ideas/recommendations?

任何人有任何想法/建议?

回答by bhatt4982

Can't help much, as there's no code. Still looking at

帮不上忙,因为没有代码。还在看

Caused by: java.lang.NullPointerException at android.app.Activity.performStop(Activity.java:3575)

引起:java.lang.NullPointerException at android.app.Activity.performStop(Activity.java:3575)

I just checked Activity.java

我刚刚检查了 Activity.java

       final int N = mManagedCursors.size();
        for (int i=0; i<N; i++) {
            ManagedCursor mc = mManagedCursors.get(i); 
            if (!mc.mReleased) {
                mc.mCursor.deactivate(); // line 3575
                mc.mReleased = true;
            }
        }

Are you sure all cursors are released properly?

你确定所有的游标都被正确释放了吗?

回答by esteewhy

Excellent explanation of what's going on inside during this situation: http://www.jjoe64.com/2011/06/how-to-fix-activityperformstop.html

在这种情况下内部发生了什么的很好的解释:http: //www.jjoe64.com/2011/06/how-to-fix-activityperformstop.html

回答by Murat

In your Activity before startManagingCursor() method is called , your Cursor is null. Dont let the Cursor be null.

在调用 startManagingCursor() 方法之前的 Activity 中,您的 Cursor 为空。不要让 Cursor 为空。