5

アプリで新しいAndroidマップを使用しようとしています。

レイアウトに(とりわけ)含まれているFragmentActivityがあります:

<LinearLayout a:id="@+id/fragment_container"
    a:layout_width="fill_parent"
    a:layout_height="100dp"
    a:layout_weight="1"/>

また、(主にサンプルプロジェクトからコピーされた)を使用してこのフラグメントを変更するボタンもあります。

if (condition) {
    fragment = new Fragment1();
} else {
    fragment = new LocationFragment();
}
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, fragment);
fragmentTransaction.commit();

LocationFragmentは次のとおりです。

public class LocationFragment extends SupportMapFragment{
private GoogleMap mMap;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    super.onCreateView(inflater, container, savedInstanceState);
    view = inflater.inflate(R.layout.map_fragment, container, false);
    setUpMapIfNeeded();
    return view;
}

@Override
public void onResume() {
    super.onResume();
    setUpMapIfNeeded();
}

private void setUpMapIfNeeded() {
    // Do a null check to confirm that we have not already instantiated the map.
    if (mMap == null) {
        // Try to obtain the map from the SupportMapFragment.
        mMap = ((SupportMapFragment)  getActivity().getSupportFragmentManager().findFragmentById(R.id.map))
                .getMap();
        // Check if we were successful in obtaining the map.
        if (mMap != null) {
            setUpMap();
        }
    }
}

private void setUpMap() {
    mMap.addMarker(new MarkerOptions().position(new LatLng(60.02532, 30.370552)).title(getString(R.string.map_current_location)));
}
}

xmlレイアウトは次のとおりです。

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/map"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      class="com.google.android.gms.maps.SupportMapFragment"/>

問題は、フラグメントを2回目にLocationFragmentに置き換えると、Androidがxmlからインフレートできないことです。オンラインでクラッシュします:

view = inflater.inflate(R.layout.map_fragment, container, false);

例外を除いて:

12-17 01:47:31.209: ERROR/AndroidRuntime(4679): FATAL EXCEPTION: main
    android.view.InflateException: Binary XML file line #4: Error inflating class fragment
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:587)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:386)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
    at my.package.LocationFragment.onCreateView(LocationFragment.java:29)
    ...

そして、私はそれを解決する方法がわかりません。複数のマップビューを使用できないという古い問題(android map v1で見つかった質問)に関連している可能性があると思います。しかし、v2とフラグメントのサポートの場合、フラグメントコンテナをマップフラグメントに置き換える方法である必要があります。

4

3 に答える 3

9

mapviewフラグメントをonDestroyViewメソッドに削除します。

Fragment fragment = (getFragmentManager().findFragmentById(R.id.mapview));
FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
ft.remove(fragment);
ft.commit();
于 2013-10-29T12:18:30.733 に答える
7

を拡張していSupportMapFragmentます。そのパターンがどれほど一般的であるかはわかりませんが、それはまったく問題ありません(これはすべて少し新しいです)。

ただし、これSupportMapFragment フラグメントであり、マップの表示方法を知っています。onCreateView()ほとんどの場合、オーバーライドするべきではありません。また、を指す要素onCreateView()を含むレイアウトファイルをオーバーライドして膨らませようとするべきではありません。<fragment>SupportMapFragment

SupportMapFragmentこれは、 MapsV2ライブラリから使用するサンプルアプリです。アクティビティは、次のようなレイアウトをロードしますSupportMapFragment

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/map"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  class="com.google.android.gms.maps.SupportMapFragment"/>
于 2012-12-16T22:16:57.297 に答える
3

私はSupportMapFragmentを拡張していませんが、上記の回答のようにXMLを使用して(通常の)フラグメント内に含めるだけですが、同じ結果が得られました... android.view.InflateException:バイナリXMLファイルの行#4:クラスを膨らませるときにエラーが発生しました起動してから2回目に含まれているフラグメントを膨らませようとしました。

この問題は、XMLからMapFragmentを拡張するのではなく、含まれているフラグメントXMLでFragmentLayoutを使用してプレースホルダーを残し、実行時にプログラムでこのMapFragmentを追加することで解決しました。完璧に動作します。

含まれているフラグメントXMLは次のようになります。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <!-- This is commented out because it crashed on second inflate.
    <fragment
        android:id="@+id/mapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_marginBottom="60dp"
        class="com.google.android.gms.maps.SupportMapFragment" />
    -->

    <FrameLayout
            android:id="@+id/mapFragmentHole"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:layout_marginBottom="60dp"
            android:layout_gravity="center_vertical|center_horizontal" /> 
    .
    .
    .

次に、含まれているフラグメントJavaコードで、次のことを行います。これは、GoogleMapOptionsを使用して表示される前にMapFragmentを構成する方法も示しています。

_view = inflater.inflate(R.layout.fragment_map, container, false);

GoogleMapOptions gmo = (new GoogleMapOptions()).zoomControlsEnabled(false).rotateGesturesEnabled(false);
mapFragment = SupportMapFragment.newInstance(gmo);
FragmentTransaction fragmentTransaction = getFragmentManager()
        .beginTransaction();
fragmentTransaction.add(R.id.mapFragmentHole, mapFragment);
fragmentTransaction.commit();

上記のコードでは、mapFragmentはメンバー変数であるため、後でアクセスして、後でGoogleMapオブジェクトをフェッチできます...

GoogleMap map = mapFragment.getMap();

ただし、onCreateViewでMapFragment switcherooを膨らませて実行すると、同じメソッド内で有効なGoogleMapオブジェクトを取得できないため、後でマップにデータを入力するまで待つ必要があります。私のコードでは、バックグラウンドスレッドを起動して、onCreateViewメソッドからデータをロードします。データが読み込まれると、UiThreadのメソッドを呼び出して、マップにアーティファクトを入力します。

お役に立てれば。幸運を!

-M。

于 2013-03-19T04:05:03.980 に答える