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

发布于 2024-10-16 07:14:07 字数 5253 浏览 3 评论 0原文

此时我感到非常沮丧。我已经研究这个问题几天了,甚至无法隔离光标问题以外的任何问题。我正在扩展 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

There is a NullPointerException at Activity.performStop(Activity.java:3808),这似乎来自此方法在 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。我的印象是,当让活动按照我的指示管理光标时,这一切都是不必要的。我将光标传递给我的自定义适配器,但我发现的示例都没有在适配器内部进行任何管理。

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

At this point I'm quite frustrated. I've been researching this for a few days and cannot even isolate anything beyond a cursor problem. I am extending ListActivity and using startManagingCursor(newcursor) in the OnCreate method. Here is the code (the database is already filled) that runs and crashes upon hitting the home button:

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;
}

Here is the error log:

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

There is a NullPointerException at Activity.performStop(Activity.java:3808), which seems to come from this method in Activity.java even though I can't cross-verify that with the line number:

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;
 }

I have tried closing and deactivating my cursor in the OnStop method as well as setting the list adapter to null. I was under the impression that none of this was necessary when letting the activity manage the cursor as I indicated. I am passing the cursor to my custom adapter but none of the examples I've found do any managing inside the adapter.

If you can help me at least narrow down what is throwing this exception, I would be grateful!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

金兰素衣 2024-10-23 07:14:07

如果这是正确的版本,则第 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;

If this is the right rev, line 3808 corresponds to:

[[ I know its not the official android source, but the line number lines up]]

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();

As far as I can tell, mc cannot ever be null. It seems that mCursor (which is final) can be null only if a null cursor was passed into startManagingCursor(). I captured my notes before for others to confirm this.

Can you log your cursor is just before you pass it into startManagingCursor()? I'd be curious what the last occurrence of that message before the crash says.

You have this line when there's a sql exception:

newcursor = null;

I wonder if that's getting into the startManagingCursor() call.


Notes:

  • startManagingCursor() creates a 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 sets final field 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;
}
  • All the internal calls to startManagingCursor() are guarded, e.g.:

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;
风为裳 2024-10-23 07:14:07

在调试器中启动您的应用程序,在 NPE 上设置异常断点,然后查看它在哪里退出。

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

Start your app in a debugger, set an exception breakpoint on NPE and then see where it is bailing out.

Should give you an idea what is going on.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文