简体   繁体   English

java.lang.IllegalStateException片段未附加到活动

[英]java.lang.IllegalStateException Fragment not attached to Activity

After I receive a response with Volley, I have to get back to the main fragment. 在收到Volley的回复后,我必须回到主要片段。 I have two different volley requests , depending on some condition, I'll call it 'a' in this example. 我有两个不同的截击请求,具体取决于某些条件,在本示例中将其称为“ a”。 The weird thing is the when a==1, popBackStack gets me successfully to the main fragment . 奇怪的是,当a == 1时,popBackStack将我成功带到主片段。 When a==0 it crashes and I receive java.lang.IllegalStateException Fragment not attached to Activity I tried creating a new main fragment (transaction.commit....) but it didn't help. 当a == 0时,它崩溃并且我收到java.lang.IllegalStateException Fragment not attached to Activity我尝试创建一个新的主要片段(transaction.commit ....),但没有帮助。

if( a == 0 )
{
    VolleyManager.add(jsnObj,
        new RequestListener() {

                    @Override
                    public <T> void onSuccess(T object) {
                        mFragmentManager.popBackStack(DataManager.BACK_STACK_KEY_MAIN_FRAGMENT, 0);
                    }
                    });
                }
else if( a==1 )
{
    VolleyManager.update(jsnObj,
        new RequestListener() {

                @Override
                public <T> void onSuccess(T object) {
                        mFragmentManager.popBackStack(DataManager.BACK_STACK_KEY_MAIN_FRAGMENT, 0);
                }   
                });
    }

Error - 错误-

  java.lang.IllegalStateException: Fragment MainFragment{6aaaf7f} not attached to Activity
  at android.app.Fragment.getResources(Fragment.java

The problem seems to be with the getResources() , but I do the same thing when a==1 and I've got no problems at all. 问题似乎出在getResources() ,但是当a == 1时我也做同样的事情,而我一点也没有问题。

It seems like that by the time AsyncTask finishes and calls onPostExecute , the MainFragment has been detached from its activity . 看来当AsyncTask完成并调用onPostExecuteMainFragment已从其activity分离出来。 So either the activity has already been destroyed or fragment was never attached. 因此,要么activity已被破坏,要么fragment从未附着。

So if fragment is not attached to the activity , it can't access resources because that requires context and fragment doesn't have but activity does. 因此,如果片段附加到activity ,则它无法访问资源,因为这需要context且片段没有,而activity却需要。

So you should check if activity is null or not before calling getResources . 因此,您应该在调用getResources之前检查activity是否为null

Update the code like this: 像这样更新代码:

if(getActivity()!=null){
    String streetFormat = getActivity().getResources().getString( R.string.address_name_string );
    ....
}

You have to cancel your requests on 您必须在取消您的请求

onDestroyView() onDestroyView()

method of the fragment or check if the fragment is already alive and added to host activity or not I'd go with something like this: 片段的方法,或者检查片段是否已经存在并添加到主机活动中,我将采用以下方法:

onDestroyView(){ Volley.cancelAllRequests() }

or 要么

onResponse(){ if(getActivity() != null && isAdded(){ // here handle the response and update views, otherwise just cache the response!}}

getResources() must be called from something that has Context , like the activity. 必须从具有Context东西(如活动getResources()中调用getResources() The fragment itself does not have the Context since it does not implement it. 片段本身没有Context因为它没有实现它。 If you're using getResources() in a fragment, you can try this: 如果在片段中使用getResources() ,则可以尝试以下操作:

String streetFormat = getActivity().getResources().getString( R.string.address_name_string );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM