android - ClassNotFoundException 在 onSaveInstanceState 中读取扩展 MapFragment 的类中的可序列化对象

标签 android supportmapfragment mapfragment

我有一个扩展 SupportMapFragment 的类,我从后端加载一些数据并显示标记。我还有另一个 fragment ,它显示与 map 上所选标记相对应的详细信息。我在 map 下方以纵向模式显示详细信息 fragment ,并以横向模式并排显示。

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 的类。我使用 onSaveInstanceState 来存储对象,这样我就不必再次从后端检索数据。但是当我翻转设备时,应用程序因 java.lang.RuntimeException: Parcelable encounteredClassNotFoundException reading a Serializable object 而崩溃。

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>

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

最佳答案

不确定为什么会这样。看起来数据正在 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");
        }
    }

}

关于android - ClassNotFoundException 在 onSaveInstanceState 中读取扩展 MapFragment 的类中的可序列化对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14612336/

相关文章:

android - 如何检测Android中的键盘动画结束?或者立即隐藏没有动画的键盘

Android Parcelable 订单

android - android fragment 中的谷歌地图v2

安卓 : Support MapFragment Returns null

java - MapFragment - 应用程序可能在其主线程上做了太多工作

android - 拦截触摸事件SlidingPaneLayout

android - 无法在 Android Oreo 上安装 CA 证书

android - react native 推送通知 onNotification 不会触发

android - 在一个应用程序中使用 2 个 map fragment 时出现问题

android - 离线使用 mapfragment 功能