Home » Android » android – Binary XML file line #8: Error inflating class fragment, Google Maps

android – Binary XML file line #8: Error inflating class fragment, Google Maps

Posted by: admin June 15, 2020 Leave a comment

Questions:

In this app, I am trying to add a functional V2 google map. I’ve looked at most of the tutorials for creating an activity, obtaining a key for the maps, and creating the xml file to try and see if I missed any step (I went through them like 4 times), and it doesn’t seem like I missed any step.

Here is the site I used:

When I run my app on my phone(since Google play services doesn’t work on the emulator), it gives me these two errors:

  02-11 22:10:54.690: E/AndroidRuntime(4526): java.lang.RuntimeException: Unable to       start activity ComponentInfo{com.example.myfirstapp/com.example.myfirstapp.ShowActivity}:       android.view.InflateException: Binary XML file line #8: Error inflating class fragment
  02-11 22:10:54.690: E/AndroidRuntime(4526): 

  Caused by: android.app.Fragment$InstantiationException: Unable to instantiate fragment       com.google.android.gms.maps.MapFragment: make sure class name exists, is public, and has an empty constructor that is public

Here is my activity:

package com.example.myfirstapp;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import com.google.android.maps.*;
import com.google.android.gms.maps.MapFragment;
public class ShowActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_map);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_map, menu);
    return true;
}

//@Override
//protected boolean isRouteDisplayed() {
    // TODO Auto-generated method stub
    //return false;
//}

    }

Sorry for the formatting but here is my xml file:

<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"
   tools:context=".ShowActivity" >

<fragment
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    class="com.google.android.gms.maps.MapFragment" />

  </RelativeLayout>

I have imported the Google API and google play services so all the imports are working, which makes me wonder why the 2nd error said MapFragment was empty.
Manifest file code:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myfirstapp"
android:versionCode="1"
android:versionName="1.0" >

<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.SUBSCRIBED_FEEDS_READ" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission        android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="com.example.myfirstapp.permission.MAPS_RECEIVE" />
<permission
    android:name="com.example.myfirstapp.permission.MAPS_RECEIVE"
    android:protectionLevel="signature" />

<uses-feature
    android:glEsVersion="0x00020000"
    android:required="true" />

<uses-sdk
    android:minSdkVersion="14"
    android:targetSdkVersion="15" />

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.myfirstapp.MainActivity"
        android:label="@string/title_activity_main" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

        <meta-data
            android:name="android.app.default_searchable"
            android:value=".SearchResultsActivity" />
    </activity>
    <activity
        android:name="com.example.myfirstapp.SearchResultsActivity"
        android:label="@string/title_activity_search_results"
        android:launchMode="singleTop" >
        <intent-filter>
            <action android:name="android.intent.action.SEARCH" />
        </intent-filter>

        <meta-data
            android:name="android.app.searchable"
            android:resource="@xml/searchable" />
    </activity>
    <activity
        android:name="com.example.myfirstapp.AddContacts"
        android:label="@string/title_activity_add_contacts" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name="com.example.myfirstapp.ContactActivity"
        android:label="@string/title_activity_contact" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name="com.example.myfirstapp.DisplayInfo"
        android:label="@string/title_activity_display_info" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

    <provider
        android:name="com.example.myfirstapp.SomeProvider"
        android:authorities="com.example.myfirstapp.SomeProvider" >
    </provider>

    <activity
        android:name="com.example.myfirstapp.EditContacts"
        android:label="@string/title_activity_edit_contacts" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name="com.example.myfirstapp.DeleteContact"
        android:label="@string/title_activity_delete_contact" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name="com.example.myfirstapp.FlightInfo"
        android:label="@string/title_activity_flight_info" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name="com.example.myfirstapp.EventInfo"
        android:label="@string/title_activity_event_info" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name="com.example.myfirstapp.ContactsActivity"
        android:label="@string/title_activity_contacts" >
    </activity>
    <activity
        android:name="com.example.myfirstapp.ShowActivity"
        android:label="@string/title_activity_map" >
    </activity>

    <meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value="removed" />
<uses-library android:name="com.google.android.maps" />
</application>

</manifest>
How to&Answers:

Try out below code:

public class ShowActivity extends android.support.v4.app.FragmentActivity {
    private GoogleMap mMap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_map);
        setUpMapIfNeeded();
    }

    @Override
    protected void onResume() {
        super.onResume();
        setUpMapIfNeeded();
    }

    private void setUpMapIfNeeded() {
        // Do a null check to confirm that we have not already instantiated the map.
        if (mMap == null) {
            // Try to obtain the map from the SupportMapFragment.
            mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
                    .getMap();
            // Check if we were successful in obtaining the map.
            if (mMap != null) {
                setUpMap();
            }
        }
    }
}

XML file code:

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
       android:id="@+id/map"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       class="com.google.android.gms.maps.SupportMapFragment"/>

Manifest file code:

    <permission
        android:name="com.example.myfirstapp.permission.MAPS_RECEIVE"
        android:protectionLevel="signature"/>
      <uses-permission android:name="com.example.myfirstapp.permission.MAPS_RECEIVE"/>
        <!-- Copied from Google Maps Library/AndroidManifest.xml. -->
      <uses-sdk
                android:minSdkVersion="8"
                android:targetSdkVersion="16"/>
        <uses-permission android:name="android.permission.INTERNET"/>
      <uses-permission         android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
<!-- External storage for caching. -->
       <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
      <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true"/>
      <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >    
         <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="removed" />
   </application>

Answer:

I am trying to deploy Google Play Services V2 (for Google Map functionalities) on Android 2.3.4. (API level 10), I had the same problem. Here is how I solved it:

  1. google-play-services_lib had to be included as a library. In Eclipse, you can do this as follows:

    • Use the Android SDK Manager to download the library.
    • Create a new Android Project from existing code where you point to google-play-services_lib in the folder you used to install the SDK. I used one level up (libproject).
    • Under Project -> Properties -> Android flag this project as Is library.
    • In your project, also open Project -> Properties -> Android and click on Add in the library section. Select the google-play-services_lib. It will appear as com_google_android_gms.

    This is important: please ensure the project build target for the library is the same as the one used for your app.

  2. Because the API version is beneath 11, SupportMapFragment is used instead of MapFragment.

Hope this helps. It has taken me at least three solid hours to work this one out.

Answer:

I followed the below steps::

Step 1: Include below line in manifest make sure you add within the application tag

    <meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value="AIzaSyA0YQoByyHKGbB2R3pSIerQCEiGlwieRas" />
    <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />

Ex::

 <application
        android:name="taxialways.user.MyApplication"
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

        <activity
            android:name="taxialways.user.Register"
            android:label="@string/title_activity_register"
            android:noHistory="true"
            android:screenOrientation="portrait" >
        </activity>

        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="AIzaSyA0YQoByyHKGbB2R3pSIerQCEiGlwieRas" />
        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />
</application>

Step 2: Make sure your activity extends FragmentActivity


Step 3: Use the mapFragment element as below in the xml

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

Answer:

Add the following lines in your manifest

<meta-data  
android:name="com.google.android.gms.version"  
android:value="@integer/google_play_services_version" />

Answer:

This problem happened to me and nothing seems to be wrong with my code ,after investigation I have noticed that the problem occurs when opening the activity after an instance run is applied during development so I just ignored it since instant run is never going to happen when the app is in the hands of a real user.!