【问题标题】:ClassNotFoundException reading a Serializable object in a class extending MapFragment in onSaveInstanceStateClassNotFoundException 在 onSaveInstanceState 中扩展 MapFragment 的类中读取 Serializable 对象
【发布时间】:2013-01-30 19:43:18
【问题描述】:

我有一个扩展 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 的类,它在派生的地图类中使用。我使用 onSaveInstanceState 来存储对象,这样我就不必再次从后端检索数据。但是当我翻转设备时,应用程序会因 java.lang.RuntimeException: Parcelable 遇到ClassNotFoundException 读取 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>

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

【问题讨论】:

  • 发布代码。始终发布代码。
  • 发布代码和堆栈跟踪。永远做这个。

标签: android supportmapfragment mapfragment


【解决方案1】:

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

}

【讨论】:

  • 谷歌搜索几天后我能找到的最佳答案!
  • 如何分配mMapView 字段?我没有找到从SupportMapFragment 获取MapView 的方法。 :(
  • @WonderCsabo 不记得了,那是 2 年前 :) 甚至可能是地图 v1。现在我直接使用MapView,没有片段,所以这个解决方案不再适用。
  • 好的,谢谢。我终于解决了这个问题,没有扩展SupportMapFragment,只是添加了FragmentManager
猜你喜欢
  • 2011-06-19
  • 2016-12-03
  • 2018-04-29
  • 1970-01-01
  • 1970-01-01
  • 2012-12-17
  • 1970-01-01
  • 2019-09-07
  • 2014-05-04
相关资源
最近更新 更多