我有一个扩展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当我翻转设备时读取可序列化的对象.
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中,将其保存到参数bundle(也被保存).这是一个例子:
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"); } } }