简体   繁体   English

如何修复android.view.InflateException:使用Google Maps v2片段时,错误放大类片段

[英]How to fix the android.view.InflateException: Error inflating class fragment when using google maps v2 fragment

I am using google map v2 fragment as a tab's content of a tabhost. 我正在使用google map v2片段作为tabhost的选项卡内容。 The first time I get the map as it should be appeared, but when I navigate with tabs and I want to return back to the tab where the map exists I get this error: 我第一次获得应该显示的地图,但是当我使用选项卡导航并且想返回到地图所在的选项卡时,出现此错误:

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)

Here's my fragment.JAVA: 这是我的片段。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;
}

And here's my layout: 这是我的布局:

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

edit (Solved) : Finally, I've found the solution. 编辑(已解决) :最后,我找到了解决方案。 The method onDestroyView() should be added as below in the JAVA file MapFragment: 方法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();
    }
}

Check this : 检查一下

Yours problem, can be in wrong (or not existed) meta-data tag in application tag of manifest file. 您的问题,可能是清单文件的应用程序标记中的元数据标记错误(或不存在)。 It should be like this: 应该是这样的:

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

UPD_0: UPD_0:

There is line in yours stackTrace: 您的stackTrace中有一行:

Duplicate id 0x7f0a0083, tag null, or parent id 0x0 with another fragment for com.google.android.gms.maps.SupportMapFragment 重复的ID 0x7f0a0083,标记为null或父ID 0x0(带有com.google.android.gms.maps.SupportMapFragment的另一个片段)

May be, when you navigate back to already created (and stored in FragmentManager) MapFragment you create another one, instead of showing old? 可能是,当您导航回已经创建(并存储在FragmentManager中)的MapFragment时,您创建了另一个,而不是显示旧的?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android Google Map API v2-错误扩充类别片段android.view.InflateException - Android Google Map API v2 - Error Inflating Class Fragment android.view.InflateException Java Android模拟器:使用Maps v2时发生崩溃(由android.view.InflateException引起:二进制XML文件第11行:错误夸大了类片段 - Java Android Emulator: Crash when using Maps v2 (Caused by android.view.InflateException: Binary XML file line #11: Error Inflating class fragment 谷歌地图视图android.view.InflateException:二进制XML文件-错误夸大类片段 - Google maps view android.view.InflateException: Binary XML File - error inflating class fragment android.view.InflateException夸大类片段的错误 - android.view.InflateException Error inflating class fragment 错误夸大类片段:android.view.InflateException - Error inflating class fragment: android.view.InflateException Android调试错误android.view.InflateException:二进制XML文件第9行:夸大类片段的错误 - Android debugging error android.view.InflateException: Binary XML file line #9: Error inflating class fragment Android-android.view.InflateException:二进制XML文件第9行:膨胀类片段的错误 - Android - android.view.InflateException: Binary XML file line #9: Error inflating class fragment 由 android 工作室制作的导航抽屉:android.view.InflateException:错误充气 class 片段 - navigation drawer made from android studio : android.view.InflateException: Error inflating class fragment Google Maps v2上的“错误夸大类片段” - “Error inflating class fragment” on Google Maps v2 Google Maps v2-错误放大类片段 - Google Maps v2 - Error inflating class fragment
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM