当我在应用程序中单击“后退”按钮时,在getResource()方法中得到了NullPointerException。 getResource用于网址中,并在glideApp中调用。
我的代码
private void setupPostUserImage(ImageView image, PostDetail detail) {
mUserPicture.setVisibility(View.VISIBLE);
String url = getActivity().getResources().getString(R.string.profile_image_path1) + "/" + detail.getUser_id_fk() + "/thumb/" + detail.getProfile_picture();
//Toast.makeText(getContext(),url,Toast.LENGTH_LONG).show();
GlideApp.with(image.getContext())
.load(url)
.placeholder(R.drawable.ic_avtar_male)
.into(image);
}
这是我的logcat崩溃报告
Process: com.ATG.World, PID: 24491
java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.support.v4.app.FragmentActivity.getResources()' on a null object reference
at com.ATG.World.fragments.ArticleDetailFragment2.setupPostUserImage(ArticleDetailFragment2.java:316)
at com.ATG.World.fragments.ArticleDetailFragment2.setData(ArticleDetailFragment2.java:301)
at com.ATG.World.fragments.ArticleDetailFragment2$1.onResponse(ArticleDetailFragment2.java:176)
at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.run(ExecutorCallAdapterFactory.java:71)
at android.os.Handler.handleCallback(Handler.java:898)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:198)
at android.app.ActivityThread.main(ActivityThread.java:6716)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
12-21 19:38:17.829 24491-24491/com.ATG.World I/Process: Sending signal. PID: 24491 SIG: 9
我应该怎么做才能解决此异常?
答案 0 :(得分:0)
我没有发表评论的声誉,所以我必须写答案。看起来您的片段未附加到活动,因此方法getActivity()返回null。看看这个答案可能会很有用getActivity() returns null in Fragment function