【问题标题】:Calling map fragment second time getting error [duplicate]第二次调用地图片段出错[重复]
【发布时间】:2014-09-06 10:28:01
【问题描述】:

我在片段中使用地图。第一次加载良好,但当我调用它时第二次出错 -

: android.view.InflateException: Binary XML file line #7: Error inflating class fragment

xml代码-

<?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"
    android:orientation="vertical" >

    <fragment
        android:id="@+id/mapfragment"
        android:layout_width="wrap_content"
        android:layout_height="200dp"
        android:layout_alignParentTop="true"
        android:layout_gravity="center_horizontal"
        class="com.google.android.gms.maps.SupportMapFragment" />

    <TextView
        android:id="@+id/title"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/mapfragment"
         />
</RelativeLayout>

java代码-

   map = ((SupportMapFragment)getActivity().getSupportFragmentManager().findFragmentById(R.id.mapfragment)).getMap();

提到了很多关于堆栈的问题,也尝试了下面的代码但没有工作 -

public void onDestroyView() {
        super.onDestroyView(); 
        Fragment fragment = (getFragmentManager().findFragmentById(R.id.mapfragment));   
        android.support.v4.app.FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
        ft.remove(fragment);
        ft.commit();
    }

使用临时解决方案 -

我通过传递地图片段整数值重新启动片段活动,以便在打开片段活动后直接加载地图片段。

【问题讨论】:

  • 第二次做什么?请发布第二次相关代码。
  • 发布的代码只是相关的。我在片段中做这一切。第一次我打开地图片段..然后在另一个片段之后当我试图打开地图片段时得到这个错误跨度>
  • 你看到这些答案了吗??? HereHere - Preffered Solution
  • @NadeemIqbal 是我已经提到过我在堆栈上提到了许多问题。请再次阅读我的问题。我已经完成了那部分
  • 我也遇到了这个错误。我只能在每次替换它时实例化 Fragment 来修复它。我在 SO 中找到的所有解决方案都对我不起作用。

标签: android android-fragments map


【解决方案1】:

改成这样:

<fragment
    android:id="@+id/mapfragment"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:clickable="true"
     />

参考此链接for Duplicate id. 它包含与您遇到的相同的错误,它对我有用,只需使用android:name 属性而不是class 属性。

【讨论】:

    猜你喜欢
    • 2015-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-04
    • 1970-01-01
    相关资源
    最近更新 更多