android - 为什么我的 Activity 在按下主页按钮时崩溃了?

标签 android cursor nullpointerexception listactivity runtimeexception

此时我非常沮丧。我已经研究了几天,甚至无法隔离游标问题以外的任何问题。我正在扩展 ListActivity 并在 OnCreate 方法中使用 startManagingCursor(newcursor) 。这是运行并在按下主页按钮时崩溃的代码(数据库已经填满):

private void loadAlbums() {
    try {
        newcursor = mDbHelper.getAlbumTitlesCursor();
        dbalbumadapter = new AlbumListCursorAdapter(this, newcursor);
        setListAdapter(dbalbumadapter);
        }
    catch (SQLiteException s) {
        newcursor = null;
        fetchalbums = new FetchAlbumsTask().execute();
        }
    current_view = ALBUMTITLE_VIEW;
}

错误日志如下:

02-03 13:41:42.379: WARN/dalvikvm(340): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
02-03 13:41:42.389: ERROR/AndroidRuntime(340): Uncaught handler: thread main exiting due to uncaught exception

02-03 13:41:42.590: ERROR/AndroidRuntime(340): java.lang.RuntimeException: Unable to stop activity {com.skip.ngRCv2/com.skip.ngRCv2.ngRC}: java.lang.RuntimeException: Unable to stop activity {com.skip.ngRCv2/com.skip.ngRCv2.MusicLibraryActivity}: java.lang.NullPointerException
02-03 13:41:42.590: ERROR/AndroidRuntime(340):     at android.app.ActivityThread.performStopActivityInner(ActivityThread.java:3227)
02-03 13:41:42.590: ERROR/AndroidRuntime(340):     at android.app.ActivityThread.handleStopActivity(ActivityThread.java:3272)
02-03 13:41:42.590: ERROR/AndroidRuntime(340):     at android.app.ActivityThread.access$2500(ActivityThread.java:119)
02-03 13:41:42.590: ERROR/AndroidRuntime(340):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1880)
02-03 13:41:42.590: ERROR/AndroidRuntime(340):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-03 13:41:42.590: ERROR/AndroidRuntime(340):     at android.os.Looper.loop(Looper.java:123)
02-03 13:41:42.590: ERROR/AndroidRuntime(340):     at android.app.ActivityThread.main(ActivityThread.java:4363)
02-03 13:41:42.590: ERROR/AndroidRuntime(340):     at java.lang.reflect.Method.invokeNative(Native Method)
02-03 13:41:42.590: ERROR/AndroidRuntime(340):     at java.lang.reflect.Method.invoke(Method.java:521)
02-03 13:41:42.590: ERROR/AndroidRuntime(340):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
02-03 13:41:42.590: ERROR/AndroidRuntime(340):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
02-03 13:41:42.590: ERROR/AndroidRuntime(340):     at dalvik.system.NativeStart.main(Native Method) 02-03 13:41:42.590: ERROR/AndroidRuntime(340): 
Caused by: java.lang.RuntimeException: Unable to stop activity {com.skip.ngRCv2/com.skip.ngRCv2.MusicLibraryActivity}: java.lang.NullPointerException
02-03 13:41:42.590: ERROR/AndroidRuntime(340):     at android.app.ActivityThread.performStopActivityInner(ActivityThread.java:3227)
02-03 13:41:42.590: ERROR/AndroidRuntime(340):     at android.app.ActivityThread.performStopActivity(ActivityThread.java:3174)
02-03 13:41:42.590: ERROR/AndroidRuntime(340):     at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:176)
02-03 13:41:42.590: ERROR/AndroidRuntime(340):     at android.app.LocalActivityManager.dispatchStop(LocalActivityManager.java:572)
02-03 13:41:42.590: ERROR/AndroidRuntime(340):     at android.app.ActivityGroup.onStop(ActivityGroup.java:79)
02-03 13:41:42.590: ERROR/AndroidRuntime(340):     at android.app.Instrumentation.callActivityOnStop(Instrumentation.java:1169)
02-03 13:41:42.590: ERROR/AndroidRuntime(340):     at android.app.Activity.performStop(Activity.java:3797)
02-03 13:41:42.590: ERROR/AndroidRuntime(340):     at android.app.ActivityThread.performStopActivityInner(ActivityThread.java:3224)
02-03 13:41:42.590: ERROR/AndroidRuntime(340):     ... 11 more
02-03 13:41:42.590: ERROR/AndroidRuntime(340): 
Caused by: java.lang.NullPointerException
02-03 13:41:42.590: ERROR/AndroidRuntime(340):     at android.app.Activity.performStop(Activity.java:3808)
02-03 13:41:42.590: ERROR/AndroidRuntime(340):     at android.app.ActivityThread.performStopActivityInner(ActivityThread.java:3224)
02-03 13:41:42.590: ERROR/AndroidRuntime(340):     ... 18 more

在 Activity.performStop(Activity.java:3808) 处有一个 NullPointerException,它似乎来自 Activity.java 中的这个方法,尽管我无法使用行号进行交叉验证:

final void performStop() {
     if (!mStopped) {
         if (mWindow != null) {
             mWindow.closeAllPanels();
         }

         mCalled = false;
         mInstrumentation.callActivityOnStop(this);
         if (!mCalled) {
             throw new SuperNotCalledException(
                 "Activity " + mComponent.toShortString() +
                 " did not call through to super.onStop()");
         }

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

         mStopped = true;
     }
     mResumed = false;
 }

我已经尝试在 OnStop 方法中关闭和停用我的光标,并将列表适配器设置为空。我的印象是,当让 Activity 按照我的指示管理光标时,这些都不是必需的。我将光标传递到我的自定义适配器,但我发现的所有示例都没有在适配器内部进行任何管理。

如果您能帮助我至少缩小引发此异常的范围,我将不胜感激!

最佳答案

如果这是正确的版本,第 3808 行对应于:

[[我知道它不是官方的 android 源,但行号是一致的]]

http://code.google.com/p/pdn-slatedroid/source/browse/trunk/eclair/frameworks/base/core/java/android/app/Activity.java?r=12#3808

mc.mCursor.deactivate();

据我所知,mc 永远不能为空。 只有当 null 游标被传递到 时,mCursor(它是最终的)似乎可以是 null startManagingCursor()我之前记录了我的笔记供其他人确认。

您能否在将光标传递到 startManagingCursor() 之前记录您的光标? 我很好奇最后一次出现的是什么崩溃前的那条消息说。

当出现 sql 异常时,你有这一行:

newcursor = null;

我想知道这是否进入了 startManagingCursor() 调用。


注意事项:

  • startManagingCursor() 创建一个 ManagedCursor:

http://code.google.com/p/pdn-slatedroid/source/browse/trunk/eclair/frameworks/base/core/java/android/app/Activity.java?r=12#1549

public void startManagingCursor(Cursor c) {
    synchronized (mManagedCursors) {
        mManagedCursors.add(new ManagedCursor(c));
    }
}
  • ManagedCursor 设置 final 字段 mCursor:

http://code.google.com/p/pdn-slatedroid/source/browse/trunk/eclair/frameworks/base/core/java/android/app/Activity.java?r=12#647

private static final class ManagedCursor {
    ManagedCursor(Cursor cursor) {
        mCursor = cursor;
        mReleased = false;
        mUpdated = false;
    }

    private final Cursor mCursor;
    private boolean mReleased;
    private boolean mUpdated;
}
  • 所有对 startManagingCursor() 的内部调用都受到保护,例如:

http://code.google.com/p/pdn-slatedroid/source/browse/trunk/eclair/frameworks/base/core/java/android/app/Activity.java?r=12#1465

if (c != null) {
    startManagingCursor(c);
}
return c;

关于android - 为什么我的 Activity 在按下主页按钮时崩溃了?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4891100/

相关文章:

java - 无法从 DrawerLayout 项目检查当前 Activity

java - 无法在recyclerview中使用exoplayer播放视频

Qt - 将光标更改为沙漏并禁用光标

mysql - Mysql存储过程中如何创建游标

Android Studio 无法识别 checkSelfPermission

android - Hello Views/From Stuff/Custom Button 教程错误

crash - Sybase ASE崩溃的更新游标

java - 什么是NullPointerException,我该如何解决?

java - 严重: Exception occurred during processing request: null java. lang.NullPointerException

java - Android Intent 按钮 NullPointerException