android - 通过重写 onCreateView() 将 SupportMapFragment 放入另一个 Fragment 的方法

标签 android google-maps-android-api-2 android-support-library

我会尝试描述一下我的情况:

我有MyFragment extends Fragment具有压倒一切的onCreateView()

MyFragment.java

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.my_fragment, null);
        //some manipulation with view
        return view;

my_fragment.xml

<LinearLayout
       .....
  >
  <com.example.widgets.MyMapWidget
                   android:id="@+id/my_map"
                   android:layout_width="match_parent"
                   android:layout_height="300dp"
                   />
 </LinearLayout>

MyMapWidget.java

public class MyMapWidget extends LinearLayout {

  public MyMapWidget(final Context context, AttributeSet attrs) {
        super(context, attrs);
       ((LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.widget_map, this, true);
...
}

widget_map.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
             >
        <!-- Some another views-->
        <fragment xmlns:android="http://schemas.android.com/apk/res/android"
                  android:id="@+id/my_map_fragment"
                  android:layout_width="match_parent"
                  android:layout_height="match_parent"
                  class="com.google.android.gms.maps.SupportMapFragment"
       />
       <!-- Some another views-->
</LinearLayout>

当我第一次展示MyFragment时- 一切都很好。但如果然后我显示另一个 fragment (带有清理后堆栈),然后 MyFragment再次 - 我得到了 Inflate Exception

Stacktrase

android.view.InflateException: Binary XML file line #151: Error inflating class <unknown>
        at android.view.LayoutInflater.createView(LayoutInflater.java:606)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:739)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:742)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:742)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:742)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
        at `com.***.****.fragments.MyFragment.onCreateView(MyFragment.java:78)`

MyFragment.java 中的第 78 行(方法onCreateView)

View view = inflater.inflate(R.layout.my_fragment, null);

正在删除MyMapWidget来自my_fragment.xml解决了问题。但我有一些问题:

  1. 为什么会发生这种情况?
  2. 我可以显示这样的 map 吗?
  3. 也许您知道另一种方式如何将 map 呈现为自己的一部分 Fragment

注意:我检查了类似的答案 question ,但无法解决我的情况。

最佳答案

您可以将 map 放在它自己的 fragment 中,如下所示:

public class GoogleMapFragment extends MapFragment {

private static final String SUPPORT_MAP_BUNDLE_KEY = "MapOptions";

public GoogleMapFragment() {
    mCallback = null;
}

public static interface OnGoogleMapFragmentListener {
    void onMapReady(GoogleMap map);
}


@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    setRetainInstance(true);
}

public static GoogleMapFragment newInstance() {
    return new GoogleMapFragment();
}

public static GoogleMapFragment newInstance(GoogleMapOptions options) {
    Bundle arguments = new Bundle();
    arguments.putParcelable(SUPPORT_MAP_BUNDLE_KEY, options);

    GoogleMapFragment fragment = new GoogleMapFragment();
    fragment.setArguments(arguments);
    return fragment;
}

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    try {
        mCallback = (OnGoogleMapFragmentListener) getActivity();
    } catch (ClassCastException e) {
        throw new ClassCastException(getActivity().getClass().getName()
                + " must implement OnGoogleMapFragmentListener");
    }
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View view = super.onCreateView(inflater, container, savedInstanceState);
    if (mCallback != null) {
        mCallback.onMapReady(getMap());
    }
    return view;
}

private OnGoogleMapFragmentListener mCallback;
}

并将其添加到您的 Activity 中,如下所示:

 // create a new map
 mapsFragment = GoogleMapFragment.newInstance();

 // Then we add it using a FragmentTransaction.
 FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
 fragmentTransaction.replace(android.R.id.content, mapsFragment, FRAGMENT_MAP_TAG);
 fragmentTransaction.commit();

让您的 Activity 实现 OnGoogleMapFragmentListener,然后当添加 map 并准备就绪时,将调用以下方法:

@Override
public void onMapReady(GoogleMap map) {
       //add markers or whatever
}

希望这对您更好地控制 map fragment 有用。

关于android - 通过重写 onCreateView() 将 SupportMapFragment 放入另一个 Fragment 的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18962372/

相关文章:

android - ActivityUnitTestCase 和 Google Maps Android API v2

android - 可以在 Google map 上隐藏兴趣点吗?

android - 点击谷歌地图标记信息窗口开始 Activity

android - 如何在Android项目配置阶段在Gradle插件中运行自定义Gradle任务?

android - Admob 在关闭广告后关闭应用程序?

android - transformClassesWithJarMergingForDebug - 重复条目 android/support/v4

Android 支持库 v4 或 v13

java - 如何在 Eclipse 中更新到最新版本的 Android 支持库?

android - 错误 : '__NDK_FPABI__' does not name a type

android - 部署 Release模式 apk 首次安装 - 必须提供 RealmMigration