restartLoader中的NullpointerException

时间:2012-08-10 12:30:05

标签: android android-activity

我正在使用我的Android应用程序,我有一个Activity显示项目列表,然后是另一个用于编辑特定项目的活动。但是现在,当我从编辑项目返回时,我在restartLoader()中得到了一个N​​ullpointerException。

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
    super.onActivityResult(requestCode, resultCode, intent);
    getLoaderManager().restartLoader(LOADER_ID, null, this);
}

我假设ResultInfo中的who不应为null,但这只是猜测。

08-10 00:44:26.427: E/AndroidRuntime(723): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=0, result=-1, data=null} to activity {duttsoft.travel/duttsoft.travel.TravelHealthActivity}: java.lang.NullPointerException
08-10 00:44:26.427: E/AndroidRuntime(723):  at android.app.ActivityThread.deliverResults(ActivityThread.java:2980)
08-10 00:44:26.427: E/AndroidRuntime(723):  at android.app.ActivityThread.handleSendResult(ActivityThread.java:3023)
08-10 00:44:26.427: E/AndroidRuntime(723):  at android.app.ActivityThread.access$1100(ActivityThread.java:123)
08-10 00:44:26.427: E/AndroidRuntime(723):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1177)
08-10 00:44:26.427: E/AndroidRuntime(723):  at android.os.Handler.dispatchMessage(Handler.java:99)
08-10 00:44:26.427: E/AndroidRuntime(723):  at android.os.Looper.loop(Looper.java:137)
08-10 00:44:26.427: E/AndroidRuntime(723):  at android.app.ActivityThread.main(ActivityThread.java:4424)
08-10 00:44:26.427: E/AndroidRuntime(723):  at java.lang.reflect.Method.invokeNative(Native Method)
08-10 00:44:26.427: E/AndroidRuntime(723):  at java.lang.reflect.Method.invoke(Method.java:511)
08-10 00:44:26.427: E/AndroidRuntime(723):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
08-10 00:44:26.427: E/AndroidRuntime(723):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
08-10 00:44:26.427: E/AndroidRuntime(723):  at dalvik.system.NativeStart.main(Native Method)
08-10 00:44:26.427: E/AndroidRuntime(723): Caused by: java.lang.NullPointerException
08-10 00:44:26.427: E/AndroidRuntime(723):  at android.app.LoaderManagerImpl.restartLoader(LoaderManager.java:651)
08-10 00:44:26.427: E/AndroidRuntime(723):  at duttsoft.travel.TravelHealthActivity.onActivityResult(TravelHealthActivity.java:136)
08-10 00:44:26.427: E/AndroidRuntime(723):  at android.app.Activity.dispatchActivityResult(Activity.java:4649)
08-10 00:44:26.427: E/AndroidRuntime(723):  at android.app.ActivityThread.deliverResults(ActivityThread.java:2976)

为什么我得到这个例外以及如何解决它,我有什么想法?

2 个答案:

答案 0 :(得分:19)

我遇到了同样的问题。我的问题是,当我最初初始化加载器时,我在onCreateLoader回调方法中返回null作为Loader。当我修复我的崩溃停止出现。尽管这个问题已经差不多一年了,但我认为如果有人在将来遇到同样的问题,我会在这里留下这个答案。

答案 1 :(得分:-5)

我的猜测是问题在于这一行:

    getLoaderManager().restartLoader(LOADER_ID, null, this);

您将null作为参数传递,然后获取NPE。尝试传递一个空的Bundle对象。