4

I have this fragment in order to present a map. When i try to get in the fragment for second time , i get this error:

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)

where line 92 is

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

this is my fragment code:

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();
    }
}

and

<?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>

the first time everything works fine, but the second the app crashes.

Doctor Jones
  • 21,196
  • 13
  • 77
  • 99
menu_on_top
  • 2,613
  • 14
  • 44
  • 71
  • What do you mean by "get in the fragment a second time"? What are you doing that might be destroying the activity? i.e. going to another activity and backing up to this one again, going to the home screen or a phone call and then returning to this activity, etc. – Tenfour04 Oct 09 '13 at 17:15
  • in my app i use a lide menu.One button in the menu is "map" and another is "news". When i first open the app i see the news fragment,i open the slide menu,press map and map shows correctly. If i go to news and try to get in map again then i get the error.Hope my description is clear :) – menu_on_top Oct 09 '13 at 17:37
  • Sorry, ignore my first answer about the singleton. I was confused a bit because you aren't following standard Java naming conventions in your newInstance method. Your classname doesn't start with a capital letter, and your local variable started with a lower-case m, which usually is reserved for member variables. – Tenfour04 Oct 09 '13 at 17:44
  • This is unrelated to your question, but why do you remove the inner map fragment in onPause? Since you are not replacing it in onResume, it is going to disappear for good if you leave the app and come back, such as after a phone call. – Tenfour04 Oct 09 '13 at 17:55
  • The error message may be the same, however this is not a duplicate of the reference question. Please read these more carefully before you mark a post as duplicate. – Lars Jul 29 '15 at 16:14

1 Answers1

0

Override the method onActivityCreated(Bundle bundle), and move these lines into it from onCreateView.

   getChildFragmentManager().beginTransaction()
       .add(R.id.map, mMapFragment).commit();
   setUpMapIfNeeded(inflatedView);
Tenfour04
  • 83,111
  • 11
  • 94
  • 154