0

I am trying to add a Google Map to a fragment in my android app and I am getting an android.view.InflateException

Background of the section of my app that is having problems:

  • I have implemented a TabLayout with ViewPager where each tab takes a fragment to display in the ViewPager
  • This uses a FragmentPagerAdapter
  • I have tested the API key etc separately and can display a basic map on my android device
  • I have tried using android:name="com.google.android.gms.maps.SupportMapFragment" and class="com.google.android.gms.maps.SupportMapFragment" in my XML layout for the map fragment

Here is a gist of some possible relevant files:

https://gist.github.com/ThrowingSpoon/dbd982c5720e4c27ed47

The error trace looks promising at these two locations:

Caused by: android.view.InflateException: Binary XML file line #7: 
Error inflating class fragment at
android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:782)

android.view.InflateException: Binary XML file line #7: 
Binary XML file line #7: Error inflating class fragment at 
android.view.LayoutInflater.inflate(LayoutInflater.java:539) at 
android.view.LayoutInflater.inflate(LayoutInflater.java:423) at 
uk.co.liammartin.shout.RespondFragment2.onCreateView(RespondFragment2.java:27)

Adding the code here:

Logcat:

Process: uk.co.liammartin.shout, PID: 17676
                                                   android.view.InflateException: Binary XML file line #7: Binary XML file line #7: Error inflating class fragment
                                                       at android.view.LayoutInflater.inflate(LayoutInflater.java:539)
                                                       at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
                                                       at uk.co.liammartin.shout.RespondFragment2.onCreateView(RespondFragment2.java:29)
                                                       at android.support.v4.app.Fragment.performCreateView(Fragment.java:1962)
                                                       at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
                                                       at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1248)
                                                       at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
                                                       at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1613)
                                                       at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:570)
                                                       at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
                                                       at android.support.v4.view.ViewPager.populate(ViewPager.java:1106)
                                                       at android.support.v4.view.ViewPager.populate(ViewPager.java:952)
                                                       at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1474)
                                                       at android.view.View.measure(View.java:18788)
                                                       at android.widget.LinearLayout.measureVertical(LinearLayout.java:901)
                                                       at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
                                                       at android.view.View.measure(View.java:18788)
                                                       at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
                                                       at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
                                                       at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135)
                                                       at android.view.View.measure(View.java:18788)
                                                       at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
                                                       at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
                                                       at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
                                                       at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
                                                       at android.view.View.measure(View.java:18788)
                                                       at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
                                                       at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
                                                       at android.view.View.measure(View.java:18788)
                                                       at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
                                                       at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
                                                       at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
                                                       at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
                                                       at android.view.View.measure(View.java:18788)
                                                       at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
                                                       at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
                                                       at com.android.internal.policy.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2643)
                                                       at android.view.View.measure(View.java:18788)
                                                       at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2100)
                                                       at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1216)
                                                       at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1452)
                                                       at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107)
                                                       at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6013)
                                                       at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
                                                       at android.view.Choreographer.doCallbacks(Choreographer.java:670)
                                                       at android.view.Choreographer.doFrame(Choreographer.java:606)
                                                       at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
                                                       at android.os.Handler.handleCallback(Handler.java:739)
                                                       at android.os.Handler.dispatchMessage(Handler.java:95)
                                                       at android.os.Looper.loop(Looper.java:148)
                                                       at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                       at java.lang.reflect.Method.invoke(Native Method)
                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                    Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
                                                       at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:782)
                                                    at

Manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="uk.co.liammartin.shout">

    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />

    <!-- Creating Permission to receive Google Maps -->
    <permission
        android:name="uk.co.liammartin.shout.MAPS_RECEIVE"
        android:protectionLevel="signature" />

    <!-- Permission to receive Google Maps -->
    <uses-permission android:name="com.arshad.map.permission.MAPS_RECEIVE" />

    <!-- Maps API needs OpenGL ES 2.0. -->
    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            <meta-data
                android:name="com.google.android.maps.v2.API_KEY"
                android:value="Removed my key but it is here" />
            <meta-data
                android:name="com.google.android.gms.version"
                android:value="@integer/google_play_services_version" />
        </activity>

        <activity
            android:name=".ShoutFilter"
            android:label="Shout Filter"
            android:theme="@style/Theme.Transparent">
        </activity>

        <activity
            android:name=".NewShout"
            android:label="Create a Shout"
            android:theme="@style/AppTheme"
            android:windowSoftInputMode="stateVisible|adjustResize">
        </activity>

        <activity android:name=".RespondToShout">
            <meta-data
                android:name="com.google.android.maps.v2.API_KEY"
                android:value="Removed my key but it is here" />
            <meta-data
                android:name="com.google.android.gms.version"
                android:value="@integer/google_play_services_version" />
        </activity>

    </application>

</manifest>

Layout:

<?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 xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/map"
        class="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="#CCFFFFFF">

        <TextView
            android:id="@+id/map_location_name"
            style="@style/Base.TextAppearance.AppCompat.Large"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="24dp"
            android:layout_marginTop="6dp"
            android:text="Novo Coffee" />

        <TextView
            android:id="@+id/map_time_distance_location"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/map_location_name"
            android:layout_centerHorizontal="true"
            android:paddingTop="10dp"
            android:text="2 minutes - 150yd - Cannon St" />

        <Button
            style="@android:style/Widget.Material.Button.Borderless"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/map_time_distance_location"
            android:layout_centerHorizontal="true"
            android:text="NAVIGATE"
            android:textColor="@color/colorPrimary" />

    </RelativeLayout>
</RelativeLayout>

but have no idea where I am going wrong. Please help!

  • please add the code where you are adding map and and complete log cat – Ishan Dec 07 '15 at 16:50
  • 1
    You're inflating a layout for a fragment, which has a fragment inside of this layout .. that is just asking for trouble. You then try to `findFragmentById` for the map fragment before this 1st fragment container has even finished being inflated! Solution: don't use fragments inside fragments – Blundell Dec 07 '15 at 16:51
  • post the xml of your fragment and activity, not just the manifest – Will Evers Dec 07 '15 at 16:52
  • All the code is here: https://gist.github.com/ThrowingSpoon/dbd982c5720e4c27ed47 @WillEvers – ThrowingSpoon Dec 07 '15 at 16:56
  • @Blundell Ahh okay, can you recommend a different approach please? – ThrowingSpoon Dec 07 '15 at 16:57
  • I think if you `extend SupportMapFragment` you will get rid of the fragments inside fragments problem. Ref: http://stackoverflow.com/questions/13735124/map-v2-supportmapfragment-inside-viewpager – Blundell Dec 07 '15 at 16:59
  • Possible duplicate of [Error opening SupportMapFragment for second time](http://stackoverflow.com/questions/14565460/error-opening-supportmapfragment-for-second-time) – android_student Dec 07 '15 at 17:21

1 Answers1

0

In your onCreateView method.

map = ((SupportMapFragment)getFragmentManager().findFragmentById(R.id.map)).getMap();

Try changing:

getFragmentManager()

to

getChildFragmentManager() or getSupportFragmentManager()

Since you have nested fragments, you probably need to pass in the child fragment manager. Also, if you're using the support v4 Fragments, you should always be using getSupportFragmentManager() instead of getFragmentManager()

android_student
  • 1,246
  • 1
  • 13
  • 32
  • This may help but the exception is happening before the FragmentManager method is even being called, meaning that the code does not even reach this line. – ThrowingSpoon Dec 07 '15 at 17:10
  • Have you tried it? If that doesn't work, then you could replace the fragment with a framelayout and add the fragment manually with a fragment transaction. fragmentTransaction.add(R.id.map, mapFragment); Being sure to use the childfragmentmanager instead of the standard fragment manager. – android_student Dec 07 '15 at 17:20
  • I have using getChildFragmentManager() to no avail, but will try this other solution soon thanks! – ThrowingSpoon Dec 07 '15 at 17:25