方向更改后NullPointerException

时间:2016-11-03 15:48:48

标签: java android orientation picasso

使用以下代码,我在旋转设备时出错。

Picasso.with(container.getContext()).load("http://image.tmdb.org/t/p/w185/" + mMovie.getPosterPath()).error(R.drawable.noimagefound).into((ImageView)rootView.findViewById(R.id.image_poster));

仅当我旋转屏幕时才会发生这种情况。如果我有一个断点并且我调试代码,那么它可以正常工作。它似乎只有在它自己运行时才会中断。

错误粘贴在下面:

Process: com.example.strahinja.popularmoviesp2, PID: 2942
              java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.strahinja.popularmoviesp2/com.example.strahinja.popularmoviesp2.activity.MovieListActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.view.ViewGroup.getContext()' on a null object reference
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
                  at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4077)
                  at android.app.ActivityThread.-wrap15(ActivityThread.java)
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1350)
                  at android.os.Handler.dispatchMessage(Handler.java:102)
                  at android.os.Looper.loop(Looper.java:148)
                  at android.app.ActivityThread.main(ActivityThread.java:5417)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
               Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.view.ViewGroup.getContext()' on a null object reference
                  at com.example.strahinja.popularmoviesp2.fragment.MovieDetailFragment.onCreateView(MovieDetailFragment.java:47)
                  at android.support.v4.app.Fragment.performCreateView(Fragment.java:2080)
                  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1108)
                  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1290)
                  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1272)
                  at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:2149)
                  at android.support.v4.app.FragmentController.dispatchActivityCreated(FragmentController.java:201)
                  at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:600)
                  at android.support.v7.app.AppCompatActivity.onStart(AppCompatActivity.java:178)
                  at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1237)
                  at android.app.Activity.performStart(Activity.java:6253)
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                  at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4077) 
                  at android.app.ActivityThread.-wrap15(ActivityThread.java) 
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1350) 
                  at android.os.Handler.dispatchMessage(Handler.java:102) 
                  at android.os.Looper.loop(Looper.java:148) 
                  at android.app.ActivityThread.main(ActivityThread.java:5417) 
                  at java.lang.reflect.Method.invoke(Native Method) 
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

有人可以告诉我我需要做些什么来解决这个问题吗?

1 个答案:

答案 0 :(得分:3)

Picasso没有抛出NullPointerException,你的代码是。

具体来说,错误指向您尝试在空对象上调用getContext()

在这种情况下,最可能的罪魁祸首是container.getContext(),这意味着您的container为空。

您没有提供设置container的代码,但似乎在重新创建活动/片段时,您不会更新该引用。