MapFragment inside DrawerLayout: Unable to instantiate*.MyMapFragment$1: make sure (...)

StackOverflow https://stackoverflow.com/questions/17191877

  •  01-06-2022
  •  | 
  •  

Question

I am trying to use a MapFragment inside one of my DrawerLayout item.

My DrawerLayout:

private void selectItem(int position) {
        switch (position) {
            (...)
            case 3:
                fragment = new MyMapFragment();
                break;
            (...)
            //THIS CODE WORKS WITH ANY OTHER FRAGMENT I CREATED!!!!!!
        }
        FragmentManager fragmentManager = getFragmentManager();
        fragmentManager.beginTransaction()
            .replace(R.id.content_frame, fragment)
            .commit();

This code works fine with all my Fragments and never crash, except for the MyMapFragment. This one (only this one) crash on device rotation while the code is very similar to my other Fragments.

The only difference is that I am using a nested Fragment:

public class MyMapFragment extends Fragment {
 public void onActivityCreated(Bundle savedInstanceState) {

        super.onActivityCreated(savedInstanceState);
        mMapFragment = new MapFragment();

        GoogleMap map = mMapFragment.getMap();

        FragmentTransaction fragmentTransaction =
            getChildFragmentManager().beginTransaction();
        fragmentTransaction.add(R.id.container, mMapFragment);
        fragmentTransaction.commit();
}

I don't what is wrong there...

Logcat:

    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.*/com.*.MainActivity}: android.app.Fragment$InstantiationException: Unable to instantiate fragment com.*.MyMapFragment$1: make sure class name exists, is public, and has an empty constructor that is public
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2194)
Was it helpful?

Solution

extends FragmentActivity instead of MapFragement

Now use SupportMapFragment

mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
                    .getMap();

map_layout.xml

<fragment
            android:id="@+id/map"

            android:name="com.google.android.gms.maps.SupportMapFragment" 

            android:layout_width="match_parent"
            android:layout_height="match_parent" />
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top