android - 第二次打开 SupportMapFragment 时出错

标签 android android-maps-v2 supportmapfragment

第二次打开我的 SupportMapFragment(Android map v2)(调用 setContentView)时,我收到以下错误:

01-28 16:27:21.374: E/AndroidRuntime(32743): FATAL EXCEPTION: main
01-28 16:27:21.374: E/AndroidRuntime(32743): android.view.InflateException: Binary XML file line #6: Error inflating class fragment
01-28 16:27:21.374: E/AndroidRuntime(32743):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
01-28 16:27:21.374: E/AndroidRuntime(32743):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
01-28 16:27:21.374: E/AndroidRuntime(32743):    at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
01-28 16:27:21.374: E/AndroidRuntime(32743):    at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
01-28 16:27:21.374: E/AndroidRuntime(32743):    at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
01-28 16:27:21.374: E/AndroidRuntime(32743):    at android.view.View.inflate(View.java:16119)
01-28 16:27:21.374: E/AndroidRuntime(32743):    at mypackage.MyView.<init>(HitsView.java:26)
01-28 16:27:21.374: E/AndroidRuntime(32743):    at mypackage.MenuListFragment.onItemClick(MenuListFragment.java:133)
01-28 16:27:21.374: E/AndroidRuntime(32743):    at android.widget.AdapterView.performItemClick(AdapterView.java:298)
01-28 16:27:21.374: E/AndroidRuntime(32743):    at android.widget.AbsListView.performItemClick(AbsListView.java:1086)
01-28 16:27:21.374: E/AndroidRuntime(32743):    at android.widget.AbsListView$PerformClick.run(AbsListView.java:2855)
01-28 16:27:21.374: E/AndroidRuntime(32743):    at android.widget.AbsListView$1.run(AbsListView.java:3529)
01-28 16:27:21.374: E/AndroidRuntime(32743):    at android.os.Handler.handleCallback(Handler.java:615)
01-28 16:27:21.374: E/AndroidRuntime(32743):    at android.os.Handler.dispatchMessage(Handler.java:92)
01-28 16:27:21.374: E/AndroidRuntime(32743):    at android.os.Looper.loop(Looper.java:137)
01-28 16:27:21.374: E/AndroidRuntime(32743):    at android.app.ActivityThread.main(ActivityThread.java:4745)
01-28 16:27:21.374: E/AndroidRuntime(32743):    at java.lang.reflect.Method.invokeNative(Native Method)
01-28 16:27:21.374: E/AndroidRuntime(32743):    at java.lang.reflect.Method.invoke(Method.java:511)
01-28 16:27:21.374: E/AndroidRuntime(32743):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
01-28 16:27:21.374: E/AndroidRuntime(32743):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
01-28 16:27:21.374: E/AndroidRuntime(32743):    at dalvik.system.NativeStart.main(Native Method)
01-28 16:27:21.374: E/AndroidRuntime(32743): Caused by: java.lang.IllegalArgumentException: Binary XML file line #6: Duplicate id 0x7f04003b, tag null, or parent id 0x0 with another fragment for com.google.android.gms.maps.SupportMapFragment
01-28 16:27:21.374: E/AndroidRuntime(32743):    at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:285)
01-28 16:27:21.374: E/AndroidRuntime(32743):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:676)
01-28 16:27:21.374: E/AndroidRuntime(32743):    ... 20 more

XML 文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:map="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <fragment
        android:id="@+id/hits_map"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        class="com.google.android.gms.maps.SupportMapFragment"
        map:mapType="normal"
        map:uiZoomControls="false"
        map:uiZoomGestures="true"
        map:cameraZoom="13"
        map:uiRotateGestures="true"
        map:uiTiltGestures="true"/>
</RelativeLayout>

MyView.class:

public class MyView extends RelativeLayout {
    private GoogleMap map;

    public MyView(Context context, FragmentActivity activity) {
        super(context);
        inflate(activity, R.layout.activity_hits, this);
        this.map = ((SupportMapFragment) activity.getSupportFragmentManager()
            .findFragmentById(R.id.hits_map)).getMap();
    }
}

我不知道这个错误是什么意思。有人可以解释一下吗?

最佳答案

更新: 作为替代解决方案(我认为更好),您可以使用描述的 MapView:here

我在使用选项卡实现时遇到了类似的问题。使用 Google Maps V2,您会被 SupportMapFragment 困住,因此无法选择使用 MapView。在 juanmeanwhile 的帖子和在这里找到的评论 #1 (https://code.google.com/p/gmaps-api-issues/issues/detail?id=5064#c1) 之间,我设法弄清楚我做错了什么。

因此,对于遇到重复 id 错误的任何其他人,请确保您以编程方式声明 fragment ,而不是在 XML 中。这可能意味着嵌套布局。

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

<!-- Lots of fancy layout -->   

<RelativeLayout
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    </RelativeLayout>
</RelativeLayout>

然后您需要以编程方式创建 fragment ,但需要仔细考虑 fragment 生命周期 (http://developer.android.com/reference/android/app/Fragment.html#getChildFragmentManager())。为确保在正确的时间创建所有内容,您的代码应如下所示(取自注释 #1)。

public class MyFragment extends Fragment {

private SupportMapFragment fragment;
private GoogleMap map;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    return inflater.inflate(R.layout.layout_with_map, container, false);
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    FragmentManager fm = getChildFragmentManager();
    fragment = (SupportMapFragment) fm.findFragmentById(R.id.map);
    if (fragment == null) {
        fragment = SupportMapFragment.newInstance();
        fm.beginTransaction().replace(R.id.map, fragment).commit();
    }
}

@Override
public void onResume() {
    super.onResume();
    if (map == null) {
        map = fragment.getMap();
        map.addMarker(new MarkerOptions().position(new LatLng(0, 0)));
    }
}
}

希望这可以节省一些时间。

关于android - 第二次打开 SupportMapFragment 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14565460/

相关文章:

android - 检查 Android 手机是否具有特定语言环境

Android ListView布局参数集

java - 如何在不扩展 fragment 类的情况下使用 getchildfragmentmanager ?

java - getMap() 方法可能返回 null?

android - 如何以编程方式在 Android 中调整麦克风音量

c# - Unity : Find out, 如果用户点击了广告

java - 如何在 Android Google Maps v2 中创建带有传感器方位的位置箭头标记?

android - 谷歌地图 API v2 : How to make markers non-clickable?

android - 如何将基本网格添加到 GoogleMap Api v2

Android - 从 SupportMapFragment 中的 Google map 中删除流量