Skip to content Skip to sidebar Skip to footer

Badparcelableexception In Google Maps Code

Running slightly modified example of Google Maps throws BadParcelableException in the Google Maps code. The LatLng class is parcelable but it cannot be found. It seems that Google

Solution 1:

Just to add to the existing answers here, I had been removing my Parcels from the saved state before calling mapView.onCreate and it was working fine.

However after adding a ViewPager to my Fragment I didn't realise that the BadParcelableException had returned and the code made it to production. It turns out that the ViewPager saves its state too and, because it's part of the Support Library, the Google Map cannot find the class to unparcel it.

So I opted to invert the process, instead of removing Parcels from the Bundle that I knew about, I opted to create a new Bundle for the map copying over only the map's state.

privatefinalstaticStringBUNDLE_KEY_MAP_STATE="mapData";

@OverridepublicvoidonSaveInstanceState(Bundle outState) {
    // Save the map state to it's own bundleBundlemapState=newBundle();
    mapView.onSaveInstanceState(mapState);
    // Put the map bundle in the main outState
    outState.putBundle(BUNDLE_KEY_MAP_STATE, mapState);
    super.onSaveInstanceState(outState);
}

@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    Viewview= inflater.inflate(R.layout.fragment_map, container, false);
    mapView = (MapView) view.findViewById(R.id.mapView);
    mapView.getMapAsync(this);

    BundlemapState=null;
    if (savedInstanceState != null) {
        // Load the map state bundle from the main savedInstanceState
        mapState = savedInstanceState.getBundle(BUNDLE_KEY_MAP_STATE);
    }

    mapView.onCreate(mapState);
    return view;
}

Solution 2:

I tried the two previous answers without success, but I found another workaround :

When saving the state, be careful to forward the MapView.onSaveInstanceState call before adding your data to the Bundle (you did it well) :

@OverridepublicvoidonSaveInstanceState(Bundle outState) {

    // Forward the call BEFORE adding our LatLng array, else it will crash :
    _mapView.onSaveInstanceState(outState);

    // Put your Parcelable in the bundle:
    outState.putParcelableArray("myLatLng", newLatLng(0, 0) );
}

When restoring the state in the onCreate / onRestore, check if the bundle is not null, if so, get your parcel, and then remove it from the bundle before forwarding the call :

@OverridepublicvoidonCreate(Bundle savedInstanceState) {

    // If the bundle is not null, get your Parcelable :LatLngmyLatLng=null;
    if(savedInstanceState != null) {

        myLatLng = (LatLng) savedInstanceState.getParcelable("myLatLng");

        // Remove your Parcelable from the bundle, else it will crash :
        savedInstanceState.remove("myLatLng");
    }

    // Forward the call :
    _mapView.onCreate(savedInstanceState);
    setUpMapIfNeeded();
}

Solution 3:

This issue as been filed on code.google.com here in case you would want to star it.

In the mean time I've manage to get around this issue by simply adding my Parcelable in to a Bundle before adding it to the final onSaveInstanceState Bundle. This works because a Bundle is a known class by the MapView's internal ClassLoader.

I've create two very small util method to do this for me. Here's the code

publicstaticParcelableunbundleParcelable(String key, Bundle src) {
    Bundle b = src.getBundle(key);
    if (b != null) {
        return b.getParcelable("bundle_parcelable_util_key");
    }
    returnnull;
}

publicstaticvoidbundleParcelable(String key, Bundle dest, Parcelable parcelable) {
    Bundle b = newBundle();
    b.putParcelable("bundle_parcelable_util_key", parcelable);
    dest.putBundle(key, b);
}

I've modified the code in one of the previous post to use my temporary solution. Here's how I use it.

@OverridepublicvoidonSaveInstanceState(Bundle outState) {

    // Forward the call BEFORE adding our LatLng array, else it will crash :
    _mapView.onSaveInstanceState(outState);

    // Put your Parcelable in the bundle:bundleParcelable("myLatLng", outState, newLatLng(0, 0));
}

@OverridepublicvoidonCreate(Bundle savedInstanceState) {
    // Forward the call :
    _mapView.onCreate(savedInstanceState);

    LatLng myLatLng = null;
    if(savedInstanceState != null) {
        // Extract your Parcelable
        myLatLng = (LatLng) unbundleParcelable("myLatLng", savedInstanceState);
    }

    setUpMapIfNeeded();
}

This should work for any custom Parcelable that you use in your project.

Solution 4:

I found a workaround (kinda). In the place when it happens just try to do it for second time. It always catches the Exception and the second time the problem does not seem to happen in this place. That worked for me.

try {
    mapIntent.putExtra(Intents.EXTRA_LATLNG, new LatLng(
        store.getLatitude(), store.getLongitude()));
} catch (BadParcelableException e) {
    mapIntent.putExtra(Intents.EXTRA_LATLNG, new LatLng(
        store.getLatitude(), store.getLongitude()));
}

Solution 5:

I have found a simpler workaround to this. Providing your Fragment or Activity's class loader to the bundle before perfoming any operations as such:

savedInstanceState.setClassLoader(getClass().getClassLoader());

This does not seem to work when passing directly into the MapFragment or MapView's onCreateView or onCreate though, so you'll manually have to parcel out the map's CameraPosition and pass a null bundle into those functions.

Post a Comment for "Badparcelableexception In Google Maps Code"