9

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がSerializableオブジェクトを読み取って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>

誰かがこの問題に遭遇した場合、またはこの問題の解決策がある場合は、私を助けてください。

4

1 に答える 1

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");
        }
    }

}
于 2013-04-12T14:12:19.963 に答える