失败保存状态:活动的SupportMapFragment {}已清除索引:-1 [英] Failure saving state: active SupportMapFragment{} has cleared index: -1

查看:114
本文介绍了失败保存状态:活动的SupportMapFragment {}已清除索引:-1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在片段内部使用了supportMapFragment,这就是为什么我使用this.getChildFragmentManager()的原因,如果这个错误,请指导我

I am using supportMapFragment inside fragment this why I am using this.getChildFragmentManager() if this wrong please guide me

public static ClinicFragment newInstance() {
        ClinicFragment fragment = new ClinicFragment();
        return fragment;
    }
    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        setTargetFragment(null , -1); // one of my tries 
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.activity_maps, null, false);

        Log.i(TAG , "onCreateView");

        mapFragment = (SupportMapFragment) this.getChildFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
        mapUtils = new MapUtils(getActivity());
        .......
        return view;
    }


 @Override
    public void onMapReady(com.google.android.gms.maps.GoogleMap googleMap) {
        mMap = googleMap;
        mMap.setMyLocationEnabled(true);
        mMap.getUiSettings().setZoomControlsEnabled(true);
        mMap.setOnMarkerClickListener(this);
    }

@Override
public void onPause() {
    super.onPause();
    mapUtils.removeUpdate(mMap); // this helper class handle change location listener   
    Log.i(TAG, "onPause");
}
@Override
public void onDestroyView() {
    super.onDestroyView();
    Log.i(TAG, "onDestroyView");

}

@Override
public void onStop() {
    super.onStop();
    Log.i(TAG, "onStop");

    killOldMap();
}

//remove map fragment when view disappeared
private void killOldMap() {
    try{
        SupportMapFragment mapFragment = ((SupportMapFragment) getChildFragmentManager()
                .findFragmentById(R.id.map));
        if(mapFragment != null) {
            FragmentManager fM = getFragmentManager();
            fM.beginTransaction().remove(mapFragment).commit();
        }
    }catch (Exception e){
        e.printStackTrace();

    }

}

我的日志

java.lang.IllegalStateException: Failure saving state: active SupportMapFragment{1ba0fd93} has cleared index: -1
at android.support.v4.app.FragmentManagerImpl.saveAllState(FragmentManager.java:1824)
at android.support.v4.app.Fragment.performSaveInstanceState(Fragment.java:2111)
at android.support.v4.app.FragmentManagerImpl.saveFragmentBasicState(FragmentManager.java:1767)
at android.support.v4.app.FragmentManagerImpl.saveAllState(FragmentManager.java:1835)
at android.support.v4.app.FragmentController.saveAllState(FragmentController.java:125)
at android.support.v4.app.FragmentActivity.onSaveInstanceState(FragmentActivity.java:523)
at android.app.Activity.performSaveInstanceState(Activity.java:1297)
at android.app.Instrumentation.callActivityOnSaveInstanceState(Instrumentation.java:1272)
at android.app.ActivityThread.callCallActivityOnSaveInstanceState(ActivityThread.java:3923)
at android.app.ActivityThread.performStopActivityInner(ActivityThread.java:3334)
at android.app.ActivityThread.handleStopActivity(ActivityThread.java:3390)
at android.app.ActivityThread.access$1100(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1307)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

我可以描述一下我尝试删除地图片段但他找不到参考的错误

I can describe my error that it try remove map fragment but he cant find it reference

我的尝试

1-经过长期研究,我发现了setTargetFragment(null , -1);方法.

1- After long research I found setTargetFragment(null , -1); method .

2-杀死onStop中的片段.

2- Kill fragment in onStop .

但是所有这些尝试都无法帮助我解决问题,任何人都可以指导我为什么会错过

but all this tries dont help me to fix my issue , any one can guide me why I miss

推荐答案

我不使用public static ClinicFragment newInstance()

我使用活动示例附带的片段.

I use fragments that are attached to activities example.

public class MyFragment extends Fragment 

我不明白您为什么要杀死您的地图片段.我从来不需要这样做,只允许自然活动生命周期管理大多数事物的破坏.

I do not understand why you are killing off your map fragment. I've never had the need to do this, and just allowed the natural activity lifecycle manage the destruction of of most things.

您还要在killOldMap之前调用super方法onDestroy,这意味着在killOldMap方法之前将调用在super方法内销毁的任务顺序.这也可能使资源悬而未决,没有附加活动.

You are also calling the super method onDestroy before your killOldMap, which means the order of tasks destroyed within the super method will be called before the killOldMap method. This can also leave resources dangling without an attached activity.

Override
public void onStop() {
    super.onStop();
    Log.i(TAG, "onStop");

    killOldMap();
}

因此,有两种方法可以解决此问题:

So there are a couple of ways to manage this:

删除片段将导致片段被破坏,而无需使用您的kill方法:

Removing the fragment will cause the fragment to be destroyed, without the need to use your kill method:

getActivity().getSupportFragmentManager().beginTransaction().remove(this)
                .commit();

正在使用fragmentTransaction.replace(R.id.yourId, fragment)

我个人比较喜欢在活动布局中使用框架布局,然后在地图和其他片段之间切换可见性的想法,我不会破坏地图,可以在那里找到我想要的任何坐标.如果我很好地管理位置服务,这不会有任何危害. 这是我使用的设计选择,绝不是法律.

Personally I like the idea of using a framelayout within my activity layout and then toggling the visibility between the map and the other fragment, I do not destroy the map, it's there to come back to with whatever co-ordinates I am seeking. This does no harm if I managing the location services well. This is a design choice I use and by no means law.

我使用该帧布局中片段的可见性来管理任何位置呼叫.创建自定义方法:

I manage any location calls with the visibility of the fragments within that framelayout. Creating a custom method:

public void hideFrames() {
    frameLayout2.setVisibility(View.GONE);
    frameLayout3.setVisibility(View.GONE);
}

通过这种方式,框架布局可以管理地图的可见性.

This way the framelayout manages the visibility of the map.

<RelativeLayout .../...
            android:id="@+id/main">

    <TextView
        android:id="@+id/t1"
    .../.../>


    <FrameLayout
        android:id="@+id/framelayout1"
        android:layout_below="@+id/t1"
        android:layout_height="fill_parent"
        android:layout_width="match_parent"
        android:visibility="gone"/>
    <FrameLayout
        android:id="@+id/framelayout2"
        android:layout_below="@+id/t1"
        android:layout_height="fill_parent"
        android:layout_width="match_parent"
        android:visibility="gone">

        <fragment
            android:id="@+id/map"
            android:layout_height="fill_parent"
            android:layout_width="match_parent"
            android:name="com.google.android.gms.maps.MapFragment"/>
    </FrameLayout>
</RelativeLayout>

,您可以在您的父活动中使用您的方法,并从您的片段中调用它们:

and you can have your methods in your parent activity and call them from your fragments:

public void replaceFragment(Fragment fragment) {
    hideFrames();
    fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.replace(R.id.YourId, fragment);
    fragmentTransaction.commit();
}

并从与活动相关的每个片段中调用它,如下所示:

and it is called from each fragment attached to the activity like so:

// create new fragment
((MainActivity) getActivity()).replaceFragment(fragment);

也:

存在一个已知问题

修复问题#6584942 IllegalStateException:失败正在保存状态...

Fix issue #6584942 IllegalStateException: Failure saving state...

...处于活动状态的AskedFragment {419494f0}已清除索引:-1

...active SuggestFragment{419494f0} has cleared index: -1

移除相同的片段然后出现问题 在完全完成删除操作之前再次添加(例如由于 到正在播放的动画.

There were issues when the same fragment was removed and then added again before completely finishing the remove (such as due to a running animation).

这篇关于失败保存状态:活动的SupportMapFragment {}已清除索引:-1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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