【问题标题】:Error in inflating map fragment膨胀地图片段时出错
【发布时间】:2015-09-30 10:14:32
【问题描述】:

我有一个导航栏,我正在使用片段,menu1fragment 是一张地图,当我尝试使用 addtobackstack 来保存我的数据时,我在夸大任何解决方案时遇到问题

MainActivity

private void displayView(int position) {
    Fragment fragment = null;
    String title = getString(R.string.app_name);
    switch (position) {
        case 0:
            fragment = new menu1_fragment();
            title = getString(R.string.title_section1);
            break;
        case 1:
            fragment = new menu2_fragment();
            title = getString(R.string.title_section2);
            break;
        case 2:
            fragment = new menu4_myfleet();
            title = getString(R.string.title_section3);
            break;
        case 3:
            fragment = new menufragment_contact();
            title = getString(R.string.title_section4);
            break;
        case 4:
            fragment = new menu5_Emergency();
            title = getString(R.string.title_section5);
            break;
        case 5:
            logoutUser();
            break;
        default:
            break;
    }

    if (fragment != null) {
        String backStateName = fragment.getClass().getName();
        FragmentManager fragmentManager = getSupportFragmentManager();
        boolean fragmentPopped = fragmentManager.popBackStackImmediate(backStateName, 0);
        if (!fragmentPopped){ //fragment not in back stack, create it.
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.replace(R.id.container_body, fragment);
            fragmentTransaction.addToBackStack(backStateName);
            fragmentTransaction.commit();
        }
      /*  FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.container_body, fragment);
        fragmentTransaction.addToBackStack(null);
        fragmentTransaction.commit();*/

        // set the toolbar title
        getSupportActionBar().setTitle(title);
    }
}

这是使地图膨胀的menu1_fragment:

  rootview = inflater.inflate(R.layout.activity_map,container,false);
    /*get map fragment and puts it in the container */
    FragmentManager fm = getChildFragmentManager();
    Fragment mapFragment = fm.findFragmentById(R.id.map);
    if (mapFragment == null) {
        mapFragment = SupportMapFragment.newInstance();
        fm.beginTransaction().replace(R.id.container_body, mapFragment, "myMap")
                .commitAllowingStateLoss();
    }


    //create the fragment that contain your vehicle
    mFragment4Container = (FrameLayout) rootview.findViewById(R.id.fragment4Container);
    mFragment4Container.setVisibility(View.GONE);
    android.support.v4.app.FragmentTransaction trans = getActivity().getSupportFragmentManager().beginTransaction();
    trans.add(mFragment4Container.getId(), new menu3_fragment());
    trans.commit();
    //create a fragmen for marker profile
    mFragment4Containeruser = (FrameLayout) rootview.findViewById(R.id.fragment4Containeruser);
    mFragment4Containeruser.setVisibility(View.GONE);
    android.support.v4.app.FragmentTransaction transprofile = getActivity().getSupportFragmentManager().beginTransaction();
    transprofile.add(mFragment4Containeruser.getId(), new user_info());
    transprofile.commit();

这是出现的错误:

android.view.InflateException:Binary XML file line #7: Error inflating class fragment at 
android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:719) at 
android.view.LayoutInflater.rInflate(LayoutInflater.java:761) at 
android.view.LayoutInflater.inflate(LayoutInflater.java:498) at
android.view.LayoutInflater.inflate(LayoutInflater.java:398) at 
com.example.fcb.insurance.menu1_fragment.onCreateView(menu1_fragment.java:120)  

Menu1_fragment `

 <fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/map"
tools:context="com.example.fcb.insurance.map"
android:name="com.google.android.gms.maps.SupportMapFragment" />

<FrameLayout
    android:id="@+id/fragment4Container"
    android:layout_width="match_parent"
    android:layout_height="400dp"
    android:background="#E2E2E2"
    android:layout_gravity="bottom"
    />
<FrameLayout
    android:id="@+id/fragment4Containeruser"
    android:layout_width="match_parent"
    android:layout_height="180dp"
    android:background="#E2E2E2"
    android:layout_gravity="bottom"
    />

<com.rey.material.widget.Button
    style="@style/Material.Drawable.Ripple.Wave.Light"
    android:layout_width="256dp"
    android:layout_height="wrap_content"
    android:text="call expert"
    android:id="@+id/buttonclose"
    android:layout_marginBottom="100dp"
    android:layout_gravity="center_horizontal|bottom"
    app:rd_enable="true"/>

<com.rey.material.widget.Button
    style="@style/Material.Drawable.Ripple.Wave.Light"
    android:layout_width="256dp"
    android:layout_height="wrap_content"
    android:text="call Tow Truck"
    android:id="@+id/button_towtruck"
    android:layout_marginBottom="50dp"
    android:layout_gravity="center_horizontal|bottom"
    app:rd_enable="true"/>
</FrameLayout>

`

主Activity.xml

  <!-- A DrawerLayout is intended to be used as the top-level content view  using match_parent for both width and height to consume the full space available. -->
<android.support.v4.widget.DrawerLayout    xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.fcb.insurance.MainActivity">


<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:id="@+id/container_toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <include
            android:id="@+id/toolbar"
            layout="@layout/toolbar" />
    </LinearLayout>

    <FrameLayout
        android:id="@+id/container_body"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />


</LinearLayout>


    <!-- android:layout_gravity="start" tells DrawerLayout to treat
     this as a sliding drawer on the left side for left-to-right
     languages and on the right side for right-to-left languages.
     If you're not building against API 17 or higher, use
     android:layout_gravity="left" instead. -->
    <!-- The drawer is given a fixed width in dp and extends the full height      of
    the container. -->
    <fragment
    android:id="@+id/fragment_navigation_drawer"
    android:name="com.example.fcb.insurance.FragmentDrawer"
    android:layout_width="@dimen/nav_drawer_width"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    app:layout="@layout/fragment_navigation_drawer"
    tools:layout="@layout/fragment_navigation_drawer"
   />




</android.support.v4.widget.DrawerLayout>

【问题讨论】:

  • 这是出现的错误 android.view.InflateException: Binary XML file line #7: Error inflating class fragment at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:719) at android.view .LayoutInflater.rInflate(LayoutInflater.java:761) 在 android.view.LayoutInflater.inflate(LayoutInflater.java:498) 在 android.view.LayoutInflater.inflate(LayoutInflater.java:398) 在 com.example.fcb.insurance。 menu1_fragment.onCreateView(menu1_fragment.java:120)
  • 请添加您失败的布局
  • 我添加了 main activity.xml 和 menu fragment.xml

标签: java android xml google-maps android-fragments


【解决方案1】:

因为您是这样在 Menu1_fragment.xml 中使用 XML 添加 SupportMapFragment

android:name="com.google.android.gms.maps.SupportMapFragment"

那么您应该从 Menu1_fragment.java 中删除此代码

 if (mapFragment == null) {
        mapFragment = SupportMapFragment.newInstance();
        fm.beginTransaction().replace(R.id.container_body, mapFragment, "myMap")
                .commitAllowingStateLoss();
    } 

反之亦然。

编辑#1

我还注意到您正在为 menu1_fragment 膨胀另一个布局

  rootview = inflater.inflate(R.layout.activity_map,container,false);

如果是R.layout.activity_map,则应该是R.layout.Menu1_fragment

【讨论】:

  • 我删除了同样的问题
  • boolean fragmentPopped = fragmentManager.popBackStackImmediate(backStateName, 0);
  • 当地图打开时任何其他片段都可以工作
  • 没有布局 menu1_fragment 我在命名标题时犯了一个错误 抱歉
【解决方案2】:

从 xml 声明中删除 android:name,并将其替换为:

class="com.google.android.gms.maps.MapFragment"

【讨论】:

  • 同样的问题我把这个 class="com.google.android.gms.maps.SupportMapFragment"
  • android:class="com.google.android.gms.maps.MapFragment" ??
  • 同样的问题任何其他解决方案
  • 任何其他片段只在地图片段中起作用
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-16
  • 2014-03-06
  • 1970-01-01
  • 2011-09-19
  • 2013-05-28
相关资源
最近更新 更多