恢复活动nullpointer异常

时间:2014-10-24 18:43:07

标签: android json android-viewpager

我有一个具有视图寻呼机的活动,并从JSON(以及几个片段)加载它的图像。 当我做某种外部活动时,比如开始一个电话或看一个网站(通过我的应用程序),当这个活动恢复时,它会给我一个空指针异常。

10-24 19:36:56.626: E/AndroidRuntime(18567): FATAL EXCEPTION: main
10-24 19:36:56.626: E/AndroidRuntime(18567): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.imagefromurl/com.example.imagefromurl.MainActivity}: java.lang.NullPointerException
10-24 19:36:56.626: E/AndroidRuntime(18567):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2373)
10-24 19:36:56.626: E/AndroidRuntime(18567):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2425)
10-24 19:36:56.626: E/AndroidRuntime(18567):    at android.app.ActivityThread.access$600(ActivityThread.java:162)
10-24 19:36:56.626: E/AndroidRuntime(18567):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1364)
10-24 19:36:56.626: E/AndroidRuntime(18567):    at android.os.Handler.dispatchMessage(Handler.java:107)
10-24 19:36:56.626: E/AndroidRuntime(18567):    at android.os.Looper.loop(Looper.java:194)
10-24 19:36:56.626: E/AndroidRuntime(18567):    at android.app.ActivityThread.main(ActivityThread.java:5422)
10-24 19:36:56.626: E/AndroidRuntime(18567):    at java.lang.reflect.Method.invokeNative(Native Method)
10-24 19:36:56.626: E/AndroidRuntime(18567):    at java.lang.reflect.Method.invoke(Method.java:525)
10-24 19:36:56.626: E/AndroidRuntime(18567):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:837)
10-24 19:36:56.626: E/AndroidRuntime(18567):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:604)
10-24 19:36:56.626: E/AndroidRuntime(18567):    at dalvik.system.NativeStart.main(Native Method)
10-24 19:36:56.626: E/AndroidRuntime(18567): Caused by: java.lang.NullPointerException
10-24 19:36:56.626: E/AndroidRuntime(18567):    at com.example.imagefromurl.FragmentOne.onCreateView(FragmentOne.java:28)
10-24 19:36:56.626: E/AndroidRuntime(18567):    at android.support.v4.app.Fragment.performCreateView(Fragment.java:1504)
10-24 19:36:56.626: E/AndroidRuntime(18567):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:942)
10-24 19:36:56.626: E/AndroidRuntime(18567):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1121)
10-24 19:36:56.626: E/AndroidRuntime(18567):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1103)
10-24 19:36:56.626: E/AndroidRuntime(18567):    at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:1901)
10-24 19:36:56.626: E/AndroidRuntime(18567):    at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:567)
10-24 19:36:56.626: E/AndroidRuntime(18567):    at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1167)
10-24 19:36:56.626: E/AndroidRuntime(18567):    at android.app.Activity.performStart(Activity.java:5132)
10-24 19:36:56.626: E/AndroidRuntime(18567):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2346)
10-24 19:36:56.626: E/AndroidRuntime(18567):    ... 11 more

我能以任何方式解决这个问题吗?

第28行:sUrl = activity.urlProfile.toString();

翻译 - 在主要活动上我运行AsynkTask,我得到这个字符串sUrl。然后在片段上我施放了活动并获得了字符串(可能你会自己想出来但是描述它并没有什么坏处)

1 个答案:

答案 0 :(得分:0)

  1. 片段丢失了activity的实例,因此字段activity为空。
  2. 或者活动urlProfile内的字段为空。
  3. 或者您将活动投放到了错误的活动中,通常使用getActivity()获取活动,或者从参数中捕获活动。
  4. 但主要的问题是,你试图在null上调用toString()导致异常。尝试用

    抓住它
    if(activity != null && activity.urlProfile != null) {
       sUrl = activity.urlProfile.toString();
    } else {
       // do something different when those objects are null
    }