第二次充气时Android map v2错误

les*_*hka 5 android android-mapview

我正在尝试在我的应用中使用新的Android地图.

我有一个FragmentActivity布局包含(除其他外):

<LinearLayout a:id="@+id/fragment_container"
    a:layout_width="fill_parent"
    a:layout_height="100dp"
    a:layout_weight="1"/>
Run Code Online (Sandbox Code Playgroud)

它还有一个按钮,用于更改此片段(主要从示例项目中复制):

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();
Run Code Online (Sandbox Code Playgroud)

和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)));
}
}
Run Code Online (Sandbox Code Playgroud)

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"/>
Run Code Online (Sandbox Code Playgroud)

问题是,当我们第二次用LocationFragment替换片段时,android无法从xml中膨胀.它崩溃了:

view = inflater.inflate(R.layout.map_fragment, container, false);
Run Code Online (Sandbox Code Playgroud)

除外:

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)
    ...
Run Code Online (Sandbox Code Playgroud)

我不知道如何解决它.我认为它可能与旧问题有关,你不能使用多个地图视图(在android map v1上找到问题).但是在v2和片段支持的情况下,它必须是用地图片段替换片段容器的方法,对吗?

iAn*_*oid 9

将mapview片段移除到onDestroyView方法中.

Fragment fragment = (getFragmentManager().findFragmentById(R.id.mapview));
FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
ft.remove(fragment);
ft.commit();
Run Code Online (Sandbox Code Playgroud)


Com*_*are 7

你正在扩展SupportMapFragment.这是完全没问题的,虽然我不确定这种模式会有多普遍(这有点太新了).

但是,它SupportMapFragment 一个片段,知道如何显示地图.你不应该覆盖onCreateView()在大多数情况下,你当然不应该被覆盖onCreateView(),并试图夸大包含一个布局文件<fragment>元素指回SupportMapFragment.

这是一个使用SupportMapFragmentMaps V2库的示例应用程序.该活动将加载有一个布局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"/>
Run Code Online (Sandbox Code Playgroud)