4

地図を表示するためにこのフラグメントがあります。フラグメントを 2 回目に取得しようとすると、次のエラーが発生します。

10-09 19:40:34.050: E/AndroidRuntime(21859): FATAL EXCEPTION: main
10-09 19:40:34.050: E/AndroidRuntime(21859): java.lang.IllegalStateException: Activity has been destroyed
10-09 19:40:34.050: E/AndroidRuntime(21859):    at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1358)
10-09 19:40:34.050: E/AndroidRuntime(21859):    at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:595)
10-09 19:40:34.050: E/AndroidRuntime(21859):    at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:574)
10-09 19:40:34.050: E/AndroidRuntime(21859):    at com.example.cultmacedonia_fragment.map.myMapFragment2.onCreateView(myMapFragment2.java:92)
10-09 19:40:34.050: E/AndroidRuntime(21859):    at android.support.v4.app.Fragment.performCreateView(Fragment.java:1478)
10-09 19:40:34.050: E/AndroidRuntime(21859):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
10-09 19:40:34.050: E/AndroidRuntime(21859):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
10-09 19:40:34.050: E/AndroidRuntime(21859):    at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
10-09 19:40:34.050: E/AndroidRuntime(21859):    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1460)
10-09 19:40:34.050: E/AndroidRuntime(21859):    at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:440)
10-09 19:40:34.050: E/AndroidRuntime(21859):    at android.os.Handler.handleCallback(Handler.java:615)
10-09 19:40:34.050: E/AndroidRuntime(21859):    at android.os.Handler.dispatchMessage(Handler.java:92)
10-09 19:40:34.050: E/AndroidRuntime(21859):    at android.os.Looper.loop(Looper.java:137)
10-09 19:40:34.050: E/AndroidRuntime(21859):    at android.app.ActivityThread.main(ActivityThread.java:4898)
10-09 19:40:34.050: E/AndroidRuntime(21859):    at java.lang.reflect.Method.invokeNative(Native Method)
10-09 19:40:34.050: E/AndroidRuntime(21859):    at java.lang.reflect.Method.invoke(Method.java:511)
10-09 19:40:34.050: E/AndroidRuntime(21859):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
10-09 19:40:34.050: E/AndroidRuntime(21859):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
10-09 19:40:34.050: E/AndroidRuntime(21859):    at dalvik.system.NativeStart.main(Native Method)

92行目はどこですか

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

これは私のフラグメントコードです:

public class myMapFragment2 extends SherlockFragment {

    private MapView mMapView;
    private static GoogleMap mMap;
    private Bundle mBundle;
    private LocationManager locationManager;
    private String provider;
    static boolean isSinglePane;
    private SupportMapFragment mMapFragment;
    double latitude = 17.385044;
    double longitude = 78.486671;

    public static SherlockFragment newInstance() {
        myMapFragment2 mFrgment = new myMapFragment2();
        return mFrgment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        GoogleMapOptions mapOptions = new GoogleMapOptions();

        Bundle bundle = getArguments();
        if (bundle != null) {

            isSinglePane = bundle.containsKey("KEY_DETAIL");

        } else {
            isSinglePane = false;
        }
        mapOptions.compassEnabled(true)
                .camera(new CameraPosition(new LatLng(latitude, longitude), 13,
                        0f, 0f));

        mMapFragment = SupportMapFragment.newInstance(mapOptions);

        Toast.makeText(getActivity().getApplicationContext(),
                String.valueOf(isSinglePane), 1000).show();

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View inflatedView = inflater.inflate(R.layout.maps3, container, false);
        setHasOptionsMenu(true);

        try {
            MapsInitializer.initialize(getActivity());
        } catch (GooglePlayServicesNotAvailableException e) {
            // TODO handle this situation
            Log.i("***GooglePlayServicesNotAvailableException", e.toString());
        }

        getChildFragmentManager().beginTransaction()
                .add(R.id.map, mMapFragment).commit();
        setUpMapIfNeeded(inflatedView);
        return inflatedView;
    }

    private void setUpMapIfNeeded(View inflatedView) {
        if (mMap == null) {
            mMap = ((MapView) inflatedView.findViewById(R.id.map)).getMap();
            if (mMap != null) {
                setUpMap();
            }
        }
    }

    private void setUpMap() {

        Marker marker = mMap.addMarker(new MarkerOptions()
                .position(new LatLng(latitude, longitude))
                .title("You are here")
                .snippet("your point")
                .icon(BitmapDescriptorFactory
                        .defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
        // create marker

        marker.showInfoWindow();
        // color icon

        // marker.icon(BitmapDescriptorFactory.fromResource(R.drawable.my_marker_icon)));
        // adding marker
        mMap.setMyLocationEnabled(true);
        mMap.getUiSettings().setCompassEnabled(true);
        locationManager = (LocationManager) getActivity()
                .getApplicationContext().getSystemService(
                        Context.LOCATION_SERVICE);
        Criteria criteria = new Criteria();
        provider = locationManager.getBestProvider(criteria, false);

    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {

        inflater.inflate(R.menu.map_menu, menu);
        super.onCreateOptionsMenu(menu, inflater);
    }

    public static void satelliteMap() {
        mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
    }

    public static void normalMap() {
        mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
    }

    public static void menuAll() {
    }

    public static void menuNearMe() {
    }

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

        if (mMap == null) {
            mMap = mMapFragment.getMap();
            if (mMap != null) {
                setUpMap();
            }
        }

    }

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

    public void onDestroyView() {
        super.onDestroyView();

        FragmentTransaction ft = getActivity().getSupportFragmentManager()
                .beginTransaction();
        ft.remove(this);
        ft.commit();
    }
}

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <com.google.android.gms.maps.MapView
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</RelativeLayout>

最初はすべて正常に動作しますが、2 回目はアプリがクラッシュします。

4

1 に答える 1

0

メソッド onActivityCreated(Bundle bundle) をオーバーライドし、これらの行を onCreateView からそれに移動します。

   getChildFragmentManager().beginTransaction()
       .add(R.id.map, mMapFragment).commit();
   setUpMapIfNeeded(inflatedView);
于 2013-10-09T17:53:22.263 に答える