如何修复android.view.InflateException:使用谷歌地图v2片段时错误膨胀类片段

时间:2015-04-19 14:55:33

标签: java android google-maps android-fragments inflate-exception

我使用google map v2片段作为标签主题的标签内容。我第一次得到应该出现的地图,但是当我使用标签导航并且我想返回到地图所在的标签时,我收到此错误:

04-19 00:02:08.623: E/AndroidRuntime(5163): android.view.InflateException: Binary XML file line #10: Error inflating class fragment
04-19 00:02:08.623: E/AndroidRuntime(5163):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
04-19 00:02:08.623: E/AndroidRuntime(5163):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
04-19 00:02:08.623: E/AndroidRuntime(5163):     at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
04-19 00:02:08.623: E/AndroidRuntime(5163):     at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
04-19 00:02:08.623: E/AndroidRuntime(5163):     at com.example.appv1.MapFragment.onCreateView(MapFragment.java:34)
04-19 00:02:08.623: E/AndroidRuntime(5163):     at android.support.v4.app.Fragment.performCreateView(Fragment.java:1504)
04-19 00:02:08.623: E/AndroidRuntime(5163):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:942)
04-19 00:02:08.623: E/AndroidRuntime(5163):     at android.support.v4.app.FragmentManagerImpl.attachFragment(FragmentManager.java:1297)
04-19 00:02:08.623: E/AndroidRuntime(5163):     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:672)
04-19 00:02:08.623: E/AndroidRuntime(5163):     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1484)
04-19 00:02:08.623: E/AndroidRuntime(5163):     at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:450)
04-19 00:02:08.623: E/AndroidRuntime(5163):     at android.os.Handler.handleCallback(Handler.java:725)
04-19 00:02:08.623: E/AndroidRuntime(5163):     at android.os.Handler.dispatchMessage(Handler.java:92)
04-19 00:02:08.623: E/AndroidRuntime(5163):     at android.os.Looper.loop(Looper.java:137)
04-19 00:02:08.623: E/AndroidRuntime(5163):     at android.app.ActivityThread.main(ActivityThread.java:5041)
04-19 00:02:08.623: E/AndroidRuntime(5163):     at java.lang.reflect.Method.invokeNative(Native Method)
04-19 00:02:08.623: E/AndroidRuntime(5163):     at java.lang.reflect.Method.invoke(Method.java:511)
04-19 00:02:08.623: E/AndroidRuntime(5163):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-19 00:02:08.623: E/AndroidRuntime(5163):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-19 00:02:08.623: E/AndroidRuntime(5163):     at dalvik.system.NativeStart.main(Native Method)
04-19 00:02:08.623: E/AndroidRuntime(5163): Caused by: java.lang.IllegalArgumentException: Binary XML file line #10: Duplicate id 0x7f0a0083, tag null, or parent id 0x0 with another fragment for com.google.android.gms.maps.SupportMapFragment
04-19 00:02:08.623: E/AndroidRuntime(5163):     at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:297)
04-19 00:02:08.623: E/AndroidRuntime(5163):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:676)

这是我的片段.JAVA:

public class MapFragment extends Fragment {

private GoogleMap map;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.activity_tab_map, container, false);

    map = ((SupportMapFragment) getFragmentManager()
            .findFragmentById(R.id.tabmap)).getMap();
    map.setMapType(GoogleMap.MAP_TYPE_NORMAL);

    return rootView;
}

这是我的布局:

<?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"
>

<fragment 
    android:id="@+id/tabmap"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentTop="true"
    android:layout_alignParentBottom="true"
    android:name="com.google.android.gms.maps.SupportMapFragment"/> 

<Button
android:id="@+id/btn_tabmapyaller"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="250dp"
android:text="Y aller" /> 

</RelativeLayout>

编辑(已解决):最后,我找到了解决方案。 onDestroyView()方法应该在JAVA文件MapFragment:

中添加如下
@Override
public void onDestroyView() {
    super.onDestroyView();
    SupportMapFragment f = (SupportMapFragment) getFragmentManager().findFragmentById(R.id.tabmap);
    if (f != null && f.isResumed()){
        getFragmentManager().beginTransaction().remove(f).commit();
    }
}

1 个答案:

答案 0 :(得分:1)

检查this

您的问题可能是清单文件的应用程序标记中的错误(或不存在)元数据标记。它应该是这样的:

<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="@integer/your_google_maps_version" />

UPD_0:

你的行中有一行:stackTrace:

  

使用com.google.android.gms.maps.SupportMapFragment的另一个片段重复ID 0x7f0a0083,标记为null或父ID为0x0

可能是,当您导航回已经创建(并存储在FragmentManager中)MapFragment时,您创建了另一个,而不是显示旧的?