BadParcelableException in google maps code

前端 未结 6 418
野性不改
野性不改 2020-12-15 22:43

Running slightly modified example of Google Maps throws BadParcelableException in the Google Maps code. The LatLng class is parcelable but it canno

6条回答
  •  暖寄归人
    2020-12-15 23:13

    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) :

    @Override
    public void onSaveInstanceState(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", new LatLng(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 :

    @Override
    public void onCreate(Bundle savedInstanceState) {
    
        // If the bundle is not null, get your Parcelable :
        LatLng myLatLng = 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();
    }
    

提交回复
热议问题