膨胀地图片段

时间:2015-09-30 10:14:33

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

我有一个导航栏,我正在使用片段,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>

2 个答案:

答案 0 :(得分: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.Menu1_fragment

,则应为R.layout.activity_map

答案 1 :(得分:0)

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

类=&#34; com.google.android.gms.maps.MapFragment&#34;