0

I have app that has map in the second menu in drawerlayout. When i try to inflate the fragment with map the app crashes.

Following is the code:

 if (rootView != null) {
        ViewGroup parent = (ViewGroup) rootView.getParent();
        if (parent != null) {
            parent.removeView(rootView);
        }
    }

    try {
        rootView = inflater.inflate(R.layout.fragment_map, container, false);


    } catch (Exception e) {

        e.printStackTrace();
    }

Following is the exception generated:

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

I've tried many solutions on stack and other platforms but in vain.

This is how i am using map fragment within parent RelativeLayout in my xml:

 <fragment 
      android:id="@+id/map"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:name="com.google.android.gms.maps.SupportMapFragment"/>

I also tried removing the map fragment in its onDestroyView methos:

@Override
 public void onDestroyView() {

     super.onDestroyView();

     SupportMapFragment fragment = ((SupportMapFragment) getChildFragmentManager()
             .findFragmentById(R.id.map));
     try{
         if (fragment != null)
             getChildFragmentManager().beginTransaction().remove(fragment).commit();
     }catch(Exception e){
     }


 }

Following is the manifest file data:

    <meta-data android:name="com.google.android.gms.version" 
        android:value="@integer/google_play_services_version" />
    <uses-library android:name="com.google.android.maps" />
    <activity
        android:name="com.app.myapp.SplashActivity"
        android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen"
        android:label="@string/app_name"
        android:noHistory="true"
        android:screenOrientation="portrait"
        android:configChanges="orientation|keyboardHidden|screenSize"
        android:launchMode="singleTop" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <activity android:name="com.app.myapp.LoginActivity"
        android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen"
              android:label="@string/app_name"
              android:launchMode="singleTop"
              android:screenOrientation="portrait"
            android:configChanges="orientation|keyboardHidden|screenSize"
               >
    </activity>

    <activity
        android:name="com.app.myapp.MainActivity"
        android:launchMode="singleTop"
        android:screenOrientation="portrait"
        android:configChanges="orientation|keyboardHidden|screenSize"
        android:label="@string/app_name" >

    </activity>

Najeebullah Shah
  • 4,164
  • 4
  • 35
  • 49
  • As per my aspect you should remove `map Fragment` from `Fragment Transaction` when you go to another `Fragment` or Screen. – M D May 07 '15 at 04:52
  • yes i have done it in OnDestroyView method but it didn't resolve my issue. The problem is that the fragment with map crashes on its first launch. – Najeebullah Shah May 07 '15 at 04:55
  • How??? Post your code...also post your manifest – M D May 07 '15 at 04:55
  • Why you used `getChildFragmentManager()` ? and How you initialized map? – M D May 07 '15 at 05:02
  • this is how i initialized map: mMap = ((SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map)).getMap(); but the problem is that the app crashes before this line. It crashes when i try to inflate the view in my map fragment. – Najeebullah Shah May 07 '15 at 05:07
  • You're initializing wrong way. can you try this one `mMap = ((SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.map)).getMap();` – M D May 07 '15 at 05:08
  • still crashes at the line: rootView = inflater.inflate(R.layout.fragment_map, container, false); – Najeebullah Shah May 07 '15 at 05:10
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/77149/discussion-between-m-d-and-najeebullah-shah). – M D May 07 '15 at 05:11
  • you still did not provide me code and i will give you answer just like playing chess in the dark – M D May 07 '15 at 05:12
  • @NajeebullahShah what api are you targeting ? – dora May 07 '15 at 05:28

1 Answers1

0

We cannot inflate static fragments in a class that extends Fragment. Instead you can replace/add the fragment(MapFragment) dynamically via code like below.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:background="@android:color/white" >

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

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:layout_gravity="top"
        android:layout_margin="0dp"
        android:layout_weight="2"
        android:background="@android:color/transparent" >

        <FrameLayout
            android:id="@+id/fragContainer"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </LinearLayout>

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

        <EditText
            android:id="@+id/inputSearch"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:drawableLeft="@android:drawable/ic_menu_search"
            android:hint="Search Tag"
            android:inputType="text" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:layout_gravity="bottom"
        android:layout_margin="0dp"
        android:layout_weight="2" >

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

            <ProgressBar
                android:id="@+id/progressBar"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical|center_horizontal"
                android:indeterminateDrawable="@drawable/my_progress_indeterminate"
                android:visibility="gone" >
            </ProgressBar>

            <ListView
                android:id="@+id/crowdmap_list"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:drawSelectorOnTop="false"
                android:listSelector="@drawable/abc_list_selector_holo_dark" />
        </LinearLayout>
    </LinearLayout>
</LinearLayout>

</RelativeLayout>

and via code now you can add/replace your mapframent to that FrameLayout like below

FragmentManager fm = getChildFragmentManager();
fm.beginTransaction().replace(R.id.fragContainer,  SupportMapFragment.newInstance(), "map").commit();
dora
  • 2,047
  • 3
  • 18
  • 20