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

时间:2013-01-30 19:43:19

标签: android supportmapfragment mapfragment

我有一个扩展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);
}

我有一个实现Serializable的类,它在派生的map类中使用。我使用onSaveInstanceState来存储对象,这样我就不必再从后端检索数据。但是当我翻转设备时,应用程序崩溃了 java.lang.RuntimeException:Parcelable visitedClassNotFoundException读取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;
    }
    ....
}

我已按如下方式定义布局:

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

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

1 个答案:

答案 0 :(得分: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");
        }
    }

}