1

Friends please healp me.. i have a simple app which show google map.. when i run the app i get the error"unfortunately the app is stoped"

The error in log cat is: Failed to inflate android.view.inflateException.Binary xml file line #2: Errror inflating class fragment at android view.LayoutInflater.createViewFromTag(LayoutInflater.java.713)

I have tryed many of the answers available from stackoverflow and many other sites. But in all that cases i get the same error.

my codes:

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mapview"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

MainActivity.java

package info.androidhive.googlemapsv2;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;

public class MainActivity extends FragmentActivity {

    private GoogleMap map;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        map = ((MapFragment) getFragmentManager()
                .findFragmentById(R.id.mapview)).getMap();
    }
}

Manifest

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

    <permission
        android:name="info.androidhive.googlemapsv2.permission.MAPS_RECEIVE"
        android:protectionLevel="signature" />

    <uses-permission android:name="info.androidhive.googlemapsv2.permission.MAPS_RECEIVE" />

    <uses-sdk
        android:minSdkVersion="12"
        android:targetSdkVersion="17" />

    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    <!-- Required to show current location -->
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

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

    <!-- Requires OpenGL ES version 2 -->
    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name">
        <activity
            android:name="info.androidhive.googlemapsv2.MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppBaseTheme">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

        <!-- Goolge API Key -->
        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="AIzaSyBZMlkOv4sj-M5JO9p6wksdax4TEjDVLgo" />
    </application>

</manifest>

Stack trace:

09-23 04:46:45.287: D/AndroidRuntime(2653): Shutting down VM
09-23 04:46:45.287: W/dalvikvm(2653): threadid=1: thread exiting with uncaught exception (group=0xb1d8db20)
09-23 04:46:45.287: E/AndroidRuntime(2653): FATAL EXCEPTION: main
09-23 04:46:45.287: E/AndroidRuntime(2653): Process: info.androidhive.googlemapsv2, PID: 2653
09-23 04:46:45.287: E/AndroidRuntime(2653): java.lang.RuntimeException: Unable to start activity ComponentInfo{info.androidhive.googlemapsv2/info.androidhive.googlemapsv2.MainActivity}: android.view.InflateException: Binary XML file line #6: Error inflating class fragment
09-23 04:46:45.287: E/AndroidRuntime(2653):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
09-23 04:46:45.287: E/AndroidRuntime(2653):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
09-23 04:46:45.287: E/AndroidRuntime(2653):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
09-23 04:46:45.287: E/AndroidRuntime(2653):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
09-23 04:46:45.287: E/AndroidRuntime(2653):     at android.os.Handler.dispatchMessage(Handler.java:102)
09-23 04:46:45.287: E/AndroidRuntime(2653):     at android.os.Looper.loop(Looper.java:136)
09-23 04:46:45.287: E/AndroidRuntime(2653):     at android.app.ActivityThread.main(ActivityThread.java:5017)
09-23 04:46:45.287: E/AndroidRuntime(2653):     at java.lang.reflect.Method.invokeNative(Native Method)
09-23 04:46:45.287: E/AndroidRuntime(2653):     at java.lang.reflect.Method.invoke(Method.java:515)
09-23 04:46:45.287: E/AndroidRuntime(2653):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
09-23 04:46:45.287: E/AndroidRuntime(2653):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
09-23 04:46:45.287: E/AndroidRuntime(2653):     at dalvik.system.NativeStart.main(Native Method)
09-23 04:46:45.287: E/AndroidRuntime(2653): Caused by: android.view.InflateException: Binary XML file line #6: Error inflating class fragment
09-23 04:46:45.287: E/AndroidRuntime(2653):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
09-23 04:46:45.287: E/AndroidRuntime(2653):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
09-23 04:46:45.287: E/AndroidRuntime(2653):     at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
09-23 04:46:45.287: E/AndroidRuntime(2653):     at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
09-23 04:46:45.287: E/AndroidRuntime(2653):     at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
09-23 04:46:45.287: E/AndroidRuntime(2653):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290)
09-23 04:46:45.287: E/AndroidRuntime(2653):     at android.app.Activity.setContentView(Activity.java:1929)
09-23 04:46:45.287: E/AndroidRuntime(2653):     at info.androidhive.googlemapsv2.MainActivity.onCreate(MainActivity.java:17)
09-23 04:46:45.287: E/AndroidRuntime(2653):     at android.app.Activity.performCreate(Activity.java:5231)
09-23 04:46:45.287: E/AndroidRuntime(2653):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
09-23 04:46:45.287: E/AndroidRuntime(2653):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
09-23 04:46:45.287: E/AndroidRuntime(2653):     ... 11 more
09-23 04:46:45.287: E/AndroidRuntime(2653): Caused by: java.lang.IllegalStateException: The meta-data tag in your app's AndroidManifest.xml does not have the right value.  Expected 5089000 but found 0.  You must have the following declaration within the <application> element:     <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />
09-23 04:46:45.287: E/AndroidRuntime(2653):     at com.google.android.gms.common.GooglePlayServicesUtil.A(Unknown Source)
09-23 04:46:45.287: E/AndroidRuntime(2653):     at com.google.android.gms.common.GooglePlayServicesUtil.isGooglePlayServicesAvailable(Unknown Source)
09-23 04:46:45.287: E/AndroidRuntime(2653):     at com.google.android.gms.maps.internal.u.I(Unknown Source)
09-23 04:46:45.287: E/AndroidRuntime(2653):     at com.google.android.gms.maps.internal.u.H(Unknown Source)
09-23 04:46:45.287: E/AndroidRuntime(2653):     at com.google.android.gms.maps.MapsInitializer.initialize(Unknown Source)
09-23 04:46:45.287: E/AndroidRuntime(2653):     at com.google.android.gms.maps.SupportMapFragment$b.jz(Unknown Source)
09-23 04:46:45.287: E/AndroidRuntime(2653):     at com.google.android.gms.maps.SupportMapFragment$b.a(Unknown Source)
09-23 04:46:45.287: E/AndroidRuntime(2653):     at com.google.android.gms.dynamic.a.a(Unknown Source)
09-23 04:46:45.287: E/AndroidRuntime(2653):     at com.google.android.gms.dynamic.a.onInflate(Unknown Source)
09-23 04:46:45.287: E/AndroidRuntime(2653):     at com.google.android.gms.maps.SupportMapFragment.onInflate(Unknown Source)
09-23 04:46:45.287: E/AndroidRuntime(2653):     at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:291)
09-23 04:46:45.287: E/AndroidRuntime(2653):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685)
09-23 04:46:45.287: E/AndroidRuntime(2653):     ... 21 more
  • Post the full stacktrace. There's the nested "caused by" exception that would be relevant. – laalto Sep 23 '14 at 08:40
  • If you are using `SupportMapFragment` in your xml file then why in your java file you are using `MapFragment`?? Just replace `MapFragment` with `SupportMapFragment`. – Piyush Sep 23 '14 at 08:52

3 Answers3

1

Place your map fragment in RelativeLayout or LinearLayout like this;

<?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">

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

 </RelativeLayout>

Then change

map = ((MapFragment) getFragmentManager()
            .findFragmentById(R.id.mapview)).getMap();

with this;

setUpMap();

setUpMap() func definition will be;

private void setUpMap(){
    if (map != null) {
        return;
    }
    map=((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.mapview)).getMap();
    if (map == null) {
        return;
    }
}

Edit

Oh guy you also miss

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

on your manifest file. Check this link

Community
  • 1
  • 1
Batuhan Coşkun
  • 2,961
  • 2
  • 31
  • 48
1

There was a missing meta-data in manifest..

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

Thank you Mr. BatuhanC who found that bug..

0

Try this..

Change this..

map = ((MapFragment) getFragmentManager()
                .findFragmentById(R.id.mapview)).getMap();

to

map = ((SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.mapview)).getMap();

and SupportMapFragment that from google play service library

import com.google.android.gms.maps.SupportMapFragment;
Hariharan
  • 24,741
  • 6
  • 50
  • 54