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

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

问题描述

我很少收到此错误,同时使一个API调用。

  java.lang.IllegalStateException:片段未连接到活动
 

我试图把code里面的 isAdded()的方法来检查片段是否正在增加它的活动,但我依然很少得到这个错误。我不明白,为什么我仍然收到此错误。我怎么能prevent呢?

它的上线示值误差 -

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

下面是我在做样本API调用。

  SAPI.getInfo(getActivity()
                新APIResponseListener(){
                    @覆盖
                    公共无效onResponse(对象响应){


                        cameraInfo =新SInfo();
                        如果(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(真正的);
                        }


                    }

                    @覆盖
                    公共无效onerror的(VolleyError错误){
                        mProgressDialog.setVisibility(View.GONE);
                        如果(错误的instanceof NoConnectionError){
                            。字符串ERRORMSG = getResources()的getString(R.string.no_internet_error_msg);
                            Toast.makeText(getActivity(),ERRORMSG,Toast.LENGTH_LONG).show();
                        }
                    }
                });
 

解决方案

此错误发生是由于两个因素的共同作用:

  • 在HTTP请求,完成时,将调用无论是 onResponse()的onError()(它的工作主线程),而无需知道活动是否仍然在前台与否。如果活动消失(用户在其他地方导航), getActivity()返回null。
  • 凌空响应是pssed作为一个匿名内部类,其中隐含持有强引用外活动EX $ P $ 类。这将导致一个经典的内存泄漏。

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

 活动活动= getActivity();
如果(活动!= NULL){

    // 等等 ...

}
 

和也,使用 isAdded()的onError()的方法,以及:

  @覆盖
公共无效onerror的(VolleyError错误){

    活性活性= getActivity();
    如果(活动= NULL和放大器;!&安培; isAdded())
        mProgressDialog.setVisibility(View.GONE);
        如果(错误的instanceof NoConnectionError){
           。字符串ERRORMSG = getResources()的getString(R.string.no_internet_error_msg);
           Toast.makeText(活性,ERRORMSG,Toast.LENGTH_LONG).show();
        }
    }
}
 

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

java.lang.IllegalStateException: Fragment  not attached to Activity

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?

Its showing error on the line-

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

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:

  • 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 ...

}

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天全站免登陆