Navvy
Navvy

Reputation: 237

Android Google Maps crashing on second load

I've looked through loads of questions, but can't seem to get any of them to work. From what I can understand, I need to destroy the map, so that it loads again next time I open it, but can't seem to work it out.

Currently what I have is below xml layout:

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

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/map">

</RelativeLayout>

</RelativeLayout>

Then in my main code I have:

public class ParkMap extends FragmentActivity implements GoogleMap.OnMarkerClickListener{

private GoogleMap mMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_park_map);
    setUpMapIfNeeded();

    mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
    mMap.setMyLocationEnabled(true);
    mMap.getUiSettings().setZoomControlsEnabled(true);
}

@Override
protected void onResume() {
    super.onResume();
    setUpMapIfNeeded();
    mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
    mMap.setMyLocationEnabled(true);
    mMap.getUiSettings().setZoomControlsEnabled(true);
}

public void setUpMapIfNeeded() {
try {
        //SupportMapFragment mMapFragment;
        mMapFragment = SupportMapFragment.newInstance();
        FragmentTransaction fragmentTransaction =
                getSupportFragmentManager().beginTransaction();
        fragmentTransaction.add(R.id.map, mMapFragment);
        fragmentTransaction.commit();

        if (mMap == null) {
            // Try to obtain the map from the SupportMapFragment.
            //mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
            //.getMap();
            mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
    // Do a null check to confirm that we have not already instantiated the map.

        if (mMap != null) {
            mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
        }
    }


    }catch (Exception e) {
        e.printStackTrace();
    }
}

The on destroy is something I tried to implement from what other posts I had read, but it isn't working. It is also crashing out when rotating the screen, therefore I believe it is just the change that it's not catching.

Thanks.

logcat:

04-08 15:36:20.043  13303-13303/com.zoome.zoomeapp E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.zoome.zoomeapp, PID: 13303
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.zoome.zoomeapp/com.zoome.zoomeapp.ParkMap}: java.lang.NullPointerException
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
        at android.app.ActivityThread.access$800(ActivityThread.java:135)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5001)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.NullPointerException
        at com.zoome.zoomeapp.ParkMap.onCreate(ParkMap.java:98)
        at android.app.Activity.performCreate(Activity.java:5231)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)

                 

Upvotes: 0

Views: 1014

Answers (3)

Ansal Ali
Ansal Ali

Reputation: 1603

I also have encountered with similar situation some days ago. You just have to comment that mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL); and try your app again. And please let me know what happens.

Upvotes: 0

CptEric
CptEric

Reputation: 907

try this approach:

have your custom Mapfragment class with the expected behaviour.

on your Fragmentactivity XML, create a FrameLayout and give it an id (map_container, as example).

create a method on your fragmentActivity that tries to replace the fragment (map_container) inside the frame with your custom class. if it fails ( throw exception ) then try to add it ( replace and add are FragmentManager methods.)

i suspect your error is created because when you visit your activity the 2nd time, it tries to add your map fragment to an already existing fragment, whereas it should be replacing it.

hope it helps. and yes, the logcat would be appreciated to see what the error is.

please, try this approach:

FragmentManager fm = null;
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
    fm = getActivity().getSupportFragmentManager();
} else {
    fm = getChildFragmentManager();
}
SupportMapFragment fragment = (SupportMapFragment)   
fragment = findFragmentById(R.id.my_map_fragment);
mMap = fragment.getMap();

Upvotes: 0

Knossos
Knossos

Reputation: 16048

If you want to destroy the fragment when the App closes, onDestroy will not always fire directly. You could move the removal code into onPause to test whether that will help you.

@Override
public void onDestroy() {
    super.onDestroy();
    MapFragment f = (MapFragment) getFragmentManager().findFragmentById(R.id.map);
    if (f != null){
        getFragmentManager().beginTransaction().remove(f).commit();
    }
}

Becomes:

@Override
public void onPause() {
    super.onPause();
    MapFragment f = (MapFragment) getFragmentManager().findFragmentById(R.id.map);
    if (f != null){
        getFragmentManager().beginTransaction().remove(f).commit();
    }
}

Upvotes: 1

Related Questions