Class not found when unmarshalling: android.support.v7.widget.Toolbar$SavedState

后端 未结 4 755
悲哀的现实
悲哀的现实 2020-12-31 12:09

i am using Maps API to create a simple android app and i get a wierd error i can\'t solve. It usually happens when i rotate my device. I\'m using google services 8.4.0

4条回答
  •  再見小時候
    2020-12-31 13:15

    I didn't like provided solutions as it imposed on my layouts and architecture.

    Here's what I did to make it work. If you look at your stacktrace the ClassNotFoundException is coming from the line on GoogleMaps. So if we just fix that, the issue is gone.

    GoogleMaps pukes/throw an error when the savedInstanceState has other items in it besides it's own. The solution is to just give GoogleMaps it's own dedicated bundle.

    // class property
    private static final String KEY_MAP_SAVED_STATE = "mapState";
    
    // class methods
    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mapView = findMapView();  // make your own method here
        Bundle mapState = (savedInstanceState != null)
                ? savedInstanceState.getBundle(KEY_MAP_SAVED_STATE): null;
        mapView.onCreate(mapState);
    }
    
    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        Bundle mapState = new Bundle();
        mapView.onSaveInstanceState(mapState);
        outState.putBundle(KEY_MAP_SAVED_STATE, mapState);
    }
    

    One thing to note is I'm not using the SupportMapFragment. I'm using a MapView directly. You may have to extend the SupportMapFragment so you can catch the hook methods and provide a blank/clean bundle

提交回复
热议问题