ClassNotFoundException读取在onSaveInstanceState中扩展MapFragment的类中的Serializable对象

Jov*_*ovy 9 android mapfragment supportmapfragment

我有一个扩展SupportMapFragment的类,我从后端加载一些数据并显示标记.我还有另一个片段,我在地图上显示与所选标记相对应的细节.我正在以纵向模式在地图下方显示细节片段,并在横向中并排显示.

public class MapDisplayFragment extends SupportMapFragment {
private ArrayList<ShowLocation> locations = null;

public void onViewCreated(View view, Bundle savedInstanceState) {
    if (savedInstanceState != null) {
    locations = (ArrayList<ShowLocation>)savedInstanceState.getSerializable("LOCATIONS");
    }
}
@Override
public void onSaveInstanceState(Bundle outState) {
  if (outState == null) {
 outState = new Bundle();
  }
  outState.putSerializable("LOCATIONS", locations);
  super.onSaveInstanceState(outState);
}
Run Code Online (Sandbox Code Playgroud)

我有一个实现Serializable的类,它在派生的map类中使用.我使用onSaveInstanceState来存储对象,这样我就不必再从后端检索数据.但应用程序崩溃与java.lang.RuntimeException:Parcelable遇到ClassClassNotFoundException当我翻转设备时读取Serializable对象.

public class ShowLocation implements Serializable {
    private String geoCodeLat = Constants.EMPTY_STRING;
    private String geoCodeLng = Constants.EMPTY_STRING;
    ....
    public String getGeoCodeLat() {
       return geoCodeLat;
    }
    public void setGeoCodeLat(String geoCodeLat) {
       this.geoCodeLat = geoCodeLat;
    }

    public String getGeoCodeLng() {
       return geoCodeLng;
    }

    public void setGeoCodeLng(String geoCodeLng) {
        this.geoCodeLng = geoCodeLng;
    }
    ....
}
Run Code Online (Sandbox Code Playgroud)

我已经定义了如下布局:

<LinearLayout
        android:id="@+id/layoutMapData"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

        <fragment
            android:id="@+id/mapFrag"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="0.5"
            class="com.test.appFragments.MapDisplayFragment" />

        <LinearLayout
            android:id="@+id/layoutDetails"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="0.5"
            android:visibility="gone" >
        </LinearLayout>
    </LinearLayout>
Run Code Online (Sandbox Code Playgroud)

如果有人遇到过这个问题,或者解决了这个问题,请帮助我.

Dmi*_*sev 13

不知道为什么会这样.看起来数据正在Google Play服务中的某个地方进行解组,并且没有关于我们课程的信息.如我错了请纠正我.

但是,有一种解决方法.而不是保存您的数据outState,将其保存到参数包(它也被保存).这是一个例子:

public MyFragment extends SupportMapFragment {

    private MapView mMapView;

    public MyFragment() {
        /*
         * We need to set bundle here.
         * Note, that if you're actually using arguments
         * then skip line below.
         */
        setArguments(new Bundle());
    }

    /* YOUR CODE HERE */

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        mMapView.onSaveInstanceState(outState);

        /* I'm using Serializable, but you can use whatever you want */
        getArguments().putSerializable("yourfield", yourValue);
    }

    @Override
    public void onCreate(Bundle savedState) {
        super.onCreate(savedState);
        mMapView.onCreate(savedState);

        if(savedState != null) {
            yourValue = getArguments.getSerializable("yourField");
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

  • 经过几天的谷歌搜索,我能找到最好的答案! (2认同)