java.lang.IllegalStateException:片段未附加到活动 [英] java.lang.IllegalStateException: Fragment not attached to Activity

查看:31
本文介绍了java.lang.IllegalStateException:片段未附加到活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在进行 API 调用时很少遇到此错误.

I am rarely getting this error while making an API call.

java.lang.IllegalStateException: Fragment  not attached to Activity

我尝试将代码放入 isAdded() 方法中以检查片段当前是否已添加到其活动中,但我仍然很少收到此错误.我不明白为什么我仍然收到这个错误.我该如何预防?

I tried putting the code inside isAdded() method to check whether fragment is currently added to its activity but still i rarely gets this error. I fail to understand why I am still getting this error. How can i prevent it?

在线显示错误-

cameraInfo.setId(getResources().getString(R.string.camera_id));

以下是我正在执行的示例 api 调用.

Below is the sample api call that i am making.

SAPI.getInfo(getActivity(),
                new APIResponseListener() {
                    @Override
                    public void onResponse(Object response) {


                        cameraInfo = new SInfo();
                        if(isAdded()) {
                            cameraInfo.setId(getResources().getString(R.string.camera_id));
                            cameraInfo.setName(getResources().getString(R.string.camera_name));
                            cameraInfo.setColor(getResources().getString(R.string.camera_color));
                            cameraInfo.setEnabled(true);
                        }


                    }

                    @Override
                    public void onError(VolleyError error) {
                        mProgressDialog.setVisibility(View.GONE);
                        if (error instanceof NoConnectionError) {
                            String errormsg = getResources().getString(R.string.no_internet_error_msg);
                            Toast.makeText(getActivity(), errormsg, Toast.LENGTH_LONG).show();
                        }
                    }
                });

推荐答案

出现这个错误的原因有两个:

This error happens due to the combined effect of two factors:

  • HTTP 请求在完成时调用 onResponse()onError()(在主线程上工作),而不知道 Activity 是否仍处于前台.如果 Activity 消失了(用户导航到别处),getActivity() 返回 null.
  • Volley Response 表示为匿名内部类,它隐式地持有对外部 Activity 类的强引用.这会导致典型的内存泄漏.
  • The HTTP request, when complete, invokes either onResponse() or onError() (which work on the main thread) without knowing whether the Activity is still in the foreground or not. If the Activity is gone (the user navigated elsewhere), getActivity() returns null.
  • The Volley Response is expressed as an anonymous inner class, which implicitly holds a strong reference to the outer Activity class. This results in a classic memory leak.

要解决这个问题,你应该总是这样做:

To solve this problem, you should always do:

Activity activity = getActivity();
if(activity != null){

    // etc ...

}

此外,在 onError() 方法中也使用 isAdded():

and also, use isAdded() in the onError() method as well:

@Override
public void onError(VolleyError error) {

    Activity activity = getActivity(); 
    if(activity != null && isAdded())
        mProgressDialog.setVisibility(View.GONE);
        if (error instanceof NoConnectionError) {
           String errormsg = getResources().getString(R.string.no_internet_error_msg);
           Toast.makeText(activity, errormsg, Toast.LENGTH_LONG).show();
        }
    }
}

这篇关于java.lang.IllegalStateException:片段未附加到活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆