Skip to content Skip to sidebar Skip to footer

Supportmapfragment Make App Crashed After Getsupportfragmentmanager()

i have a problem to access SupportMapFragment, in this part of my code, its always returning NPE, googleMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.

Solution 1:

for your requirement you can see this link for GoogleMap v2 with ActionBarGit hub link

https://github.com/ddewaele/GoogleMapsV2WithActionBarSherlock

or u can use MapView in your activity

https://developers.google.com/maps/documentation/android/map#mapview

Solution 2:

Try like this:

if (googleMap == null) {
            android.support.v4.app.FragmentManagerfragmentManager= getSupportFragmentManager();
            googleMap = ((SupportMapFragment) fragmentManager.findFragmentById(R.id.map)).getMap();
            Log.i("Google map", "Successfully Googlemap is opened");
}

Note: You need to extend FragmentActivity instead of ActionBarActivity.

Solution 3:

I suggest to you to do like following :

XML :

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin" >

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

</FrameLayout>

Your MainActivity :

private GoogleMap googleMap;
    private MapView mMapView;

    @OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_show_post);

        finalViewcontrolsView= findViewById(R.id.fullscreen_content_controls);
        finalViewcontentView= findViewById(R.id.map);

        initilizeMap(); 
    }

    privatevoidinitilizeMap() {
        if (googleMap == null) {
            googleMap = ((MapFragment) getFragmentManager().findFragmentById(
                    R.id.map)).getMap();

            // check if map is created successfully or notif (googleMap == null) {
                Toast.makeText(getApplicationContext(),
                        "Sorry! unable to create maps", Toast.LENGTH_SHORT)
                        .show();
            }
        }
    }

    @OverrideprotectedvoidonResume() {
        super.onResume();
        initilizeMap();
    }

You override onResume Method and you initilize the Map. This should work ;)

Solution 4:

private GoogleMap map;
private SupportMapFragment mapFragment;

// in onCreate() or in onStart() do some thing like this

fragmentManager = getSupportFragmentManager();
    mapFragment = (SupportMapFragment) fragmentManager
            .findFragmentById(R.id.maps_parks);
    map = mapFragment.getMap();

Solution 5:

Try This

In xml

<?xml version="1.0" encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical" ><fragmentandroid:id="@+id/map"android:layout_width="match_parent"android:layout_height="match_parent"class="com.google.android.gms.maps.MapFragment"
         /></LinearLayout>

In Activity:-

publicclassActivityMainextendsActivity {
        // Google MappublicGoogleMap googleMap;

    @OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        try {
            Log.e("loading map. . . .", "loading map. . . ");
            // Loading mapinitilizeMap();

        } catch (Exception e) {
            Log.e("catch. . .", "catch. . .");
            e.printStackTrace();
        }
        /*
         * MapFunctionality mf = new MapFunctionality(); mf.currentLoc();
         * mf.addMarker();
         */
    }

    /**
     * function to load map. If map is not created it will create it for you
     * */privatevoidinitilizeMap() {
        // MapFunctionality mf = new MapFunctionality();Log.e("initializing map. . . ", "initializing map. . ");
        if (googleMap == null) {
            googleMap = ((MapFragment) getFragmentManager().findFragmentById(
                    R.id.map)).getMap();

            // check if map is created successfully or notif (googleMap == null) {
                Toast.makeText(getApplicationContext(),
                        "Sorry! unable to create maps", Toast.LENGTH_SHORT)
                        .show();
            }
            // mf.currentLoc();// mf.addMarker();
        }
    }

    @OverridepublicbooleanonCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.activity_main, menu);
        returntrue;
    }

    @OverrideprotectedvoidonResume() {
        super.onResume();
        initilizeMap();
    }
}

Follow these Link 1 and Link 2 Hope it will help you to sort out with your problem Good Luck

Post a Comment for "Supportmapfragment Make App Crashed After Getsupportfragmentmanager()"