切换片段 - 谷歌地图片段V2 - 活动已被破坏错误

时间:2013-03-05 19:31:06

标签: android android-fragments android-activity mapfragment supportmapfragment

我有一个地图活动(SherlockFragmentActivity)加载在地图片段中,该片段包含2个按钮并在显示2个不同片段之间切换(GoogleMapFragment& ScaleImageViewFragment)

Screen Shot

当我点击“参展商地图”按钮时,它会替换该片段,但是当我点击回来时,我得到一个“java.lang.IllegalStateException:Activity已被销毁”错误

ERROR/AndroidRuntime(5768): FATAL EXCEPTION: main
    java.lang.IllegalStateException: Activity has been destroyed
    at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1342)
    at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:595)
    at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:574)
    at com.takeit2eleven.towniemeeting.android.fragments.GoogleMapFragment.onCreateView(GoogleMapFragment.java:42)
    at android.support.v4.app.Fragment.performCreateView(Fragment.java:1460)
    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:911)
    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
    at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
    at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:429)
    at android.os.Handler.handleCallback(Handler.java:615)
    at android.os.Handler.dispatchMessage(Handler.java:92)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:4745)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
    at dalvik.system.NativeStart.main(Native Method)

这是我的MapsFragment.java文件:

public class MapsFragment extends SherlockFragment {

Button mHotelMapButton;
Button mExhibitorButton;
GoogleMapFragment mGoogleMapFragment;
ScaleImageViewFragment mScaleImageViewFragment;
static boolean mShowingGoogleMap = true;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (savedInstanceState != null) {
        mShowingGoogleMap = savedInstanceState.getBoolean("showingGoogleMap");
    }

    mGoogleMapFragment = new GoogleMapFragment();
    mScaleImageViewFragment = new ScaleImageViewFragment();

}

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putBoolean("showingGoogleMap", mShowingGoogleMap);

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.maps_fragment, null);
    mHotelMapButton = (Button) view.findViewById(R.id.hotel_button);
    mExhibitorButton = (Button) view.findViewById(R.id.exhititor_map_button);

    mExhibitorButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mShowingGoogleMap = false;
            showExhibitorFragment();
        }
    });

    mHotelMapButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mShowingGoogleMap = true;
            showGoogleMapFragment();
        }
    });

    return view;

}

@Override
public void onPause() {
    super.onPause();
    getChildFragmentManager().beginTransaction()
            .remove(mGoogleMapFragment)
            .remove(mScaleImageViewFragment).commit();
}

@Override
public void onResume() {
    super.onResume();


    if(mShowingGoogleMap){
        showGoogleMapFragment();

    }else{
        showExhibitorFragment();
    }
}

private void showExhibitorFragment(){
    getChildFragmentManager().beginTransaction()
            .remove(mGoogleMapFragment)
            .remove(mScaleImageViewFragment)
            .replace(R.id.fragment_placeholder, mScaleImageViewFragment, "scaleImage")
            .commit();
}
private void showGoogleMapFragment(){
    getChildFragmentManager().beginTransaction()
            .remove(mGoogleMapFragment)
            .remove(mScaleImageViewFragment)
            .replace(R.id.fragment_placeholder, mGoogleMapFragment, "mapFragment")
            .commit();
}
}

和我的GoogleMapFragment.java文件:

public class GoogleMapFragment extends Fragment {
private SupportMapFragment mMapFragment;
private GoogleMap mMap;
private final LatLng PLANET_HOLLYWOOD = new LatLng(36.1100, -115.1710);

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    GoogleMapOptions mapOptions = new GoogleMapOptions();
    mapOptions.compassEnabled(true).camera(new CameraPosition(PLANET_HOLLYWOOD, 13, 0f, 0f));
    mMapFragment = SupportMapFragment.newInstance(mapOptions);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    View view = inflater.inflate(R.layout.google_map, container, false);

    getChildFragmentManager()
            .beginTransaction()
            .add(R.id.map_holder, mMapFragment)
            .commit();

    return view;

}

@Override
public void onPause() {
    super.onPause();
    getChildFragmentManager().beginTransaction()
            .remove(mMapFragment)
            .commit();
}


@Override
public void onResume() {
    super.onResume();

    mMap = mMapFragment.getMap();
    if(mMap != null){
        mMap.addMarker(new MarkerOptions().position(PLANET_HOLLYWOOD).title("Planet Hollywood"));
    }
}
}

我一直在撞墙,试图弄清楚这个错误以及如何避免/修复它。

更新

顺便提一下,如果我切换到:

getActivity().getSupportFragmentManager()
            .beginTransaction()
            .add(R.id.map_holder, mMapFragment)
            .commit();
在GoogleMapFragment中的

然后我没有得到错误,但我无法抓住地图来设置制造商...这似乎是竞争条件

1 个答案:

答案 0 :(得分:14)

如果删除onDestroyView()中的所有嵌套片段,则可以解决此问题:

public void onDestroyView() 
{
        super.onDestroyView(); 
        Fragment fragment = (getFragmentManager().findFragmentById(R.id.map));  
        FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
        ft.remove(fragment);
        ft.commit();
}

并像往常一样在onCreateView()

中给他们充气
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    return inflater.inflate(R.layout.map, container, false);
}