Farouk Chamsi Bacha
Farouk Chamsi Bacha

Reputation: 1

Error in inflating map fragment

I have a navigation bar and I am working with fragment, menu1fragment is a map, when I tried to use addtobackstack to save my data I have a problem in inflating any solution

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);
    }
}

This is menu1_fragment that inflate the map:

  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();

This is the error that appears:

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>

`

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

Upvotes: 0

Views: 308

Answers (2)

msamhoury
msamhoury

Reputation: 333

Since you are adding the SupportMapFragment using XML in the Menu1_fragment.xml like that

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

then you should remove this code from Menu1_fragment.java

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

or vice versa.

Edit #1

I also noticed that you are inflating another layout for your menu1_fragment

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

It should be R.layout.Menu1_fragment instead if R.layout.activity_map

Upvotes: 1

Shine
Shine

Reputation: 3818

remove android:name from xml declaration, and replace it with:

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

Upvotes: 0

Related Questions