为什么我的活动在按下主页按钮时会崩溃?

时间:2011-02-03 20:06:51

标签: 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方法中关闭和停用光标,并将列表适配器设置为null。我的印象是,当我按照指示让活动管理光标时,这一切都不是必需的。我将光标传递给我的自定义适配器,但我发现的任何示例都没有在适配器内进行任何管理。

如果你能帮助我至少缩小抛出这个例外的内容,我将不胜感激!

2 个答案:

答案 0 :(得分:9)

如果这是正确的转速,则行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永远不会为空。 只有当mCursor光标传递到null时,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;

答案 1 :(得分:0)

在调试器中启动你的应用程序,在NPE上设置一个异常断点,然后看看它在哪里拯救。

应该让你知道发生了什么。