Google Map is not loading due to inflate exception

2019-02-20 22:08发布

问题:

I am using Google Maps v2 in my application. The key is correctly generated, all the permissions exist in manifest. I followed the tutorial of Google Maps v2. I followed each and every step but it's not working. Please Help

Log

03-04 01:25:23.145: E/Trace(19467): error opening trace file: No such file or directory (2)
03-04 01:25:23.293: E/AndroidRuntime(19467): FATAL EXCEPTION: main
03-04 01:25:23.293: E/AndroidRuntime(19467): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.vogella.android.locationapi.maps/com.appscourt.jooging.track.map.health.MainActivity}: android.view.InflateException: Binary XML file line #11: Error inflating class fragment
03-04 01:25:23.293: E/AndroidRuntime(19467):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2136)
03-04 01:25:23.293: E/AndroidRuntime(19467):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2174)
03-04 01:25:23.293: E/AndroidRuntime(19467):    at android.app.ActivityThread.access$700(ActivityThread.java:141)
03-04 01:25:23.293: E/AndroidRuntime(19467):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1267)
03-04 01:25:23.293: E/AndroidRuntime(19467):    at android.os.Handler.dispatchMessage(Handler.java:99)
03-04 01:25:23.293: E/AndroidRuntime(19467):    at android.os.Looper.loop(Looper.java:137)
03-04 01:25:23.293: E/AndroidRuntime(19467):    at android.app.ActivityThread.main(ActivityThread.java:5059)
03-04 01:25:23.293: E/AndroidRuntime(19467):    at java.lang.reflect.Method.invokeNative(Native Method)
03-04 01:25:23.293: E/AndroidRuntime(19467):    at java.lang.reflect.Method.invoke(Method.java:511)
03-04 01:25:23.293: E/AndroidRuntime(19467):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)
03-04 01:25:23.293: E/AndroidRuntime(19467):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
03-04 01:25:23.293: E/AndroidRuntime(19467):    at dalvik.system.NativeStart.main(Native Method)
03-04 01:25:23.293: E/AndroidRuntime(19467): Caused by: android.view.InflateException: Binary XML file line #11: Error inflating class fragment
03-04 01:25:23.293: E/AndroidRuntime(19467):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
03-04 01:25:23.293: E/AndroidRuntime(19467):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
03-04 01:25:23.293: E/AndroidRuntime(19467):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:749)
03-04 01:25:23.293: E/AndroidRuntime(19467):    at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
03-04 01:25:23.293: E/AndroidRuntime(19467):    at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
03-04 01:25:23.293: E/AndroidRuntime(19467):    at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
03-04 01:25:23.293: E/AndroidRuntime(19467):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:260)
03-04 01:25:23.293: E/AndroidRuntime(19467):    at android.app.Activity.setContentView(Activity.java:1893)
03-04 01:25:23.293: E/AndroidRuntime(19467):    at com.appscourt.jooging.track.map.health.MainActivity.onCreate(MainActivity.java:20)
03-04 01:25:23.293: E/AndroidRuntime(19467):    at android.app.Activity.performCreate(Activity.java:5058)
03-04 01:25:23.293: E/AndroidRuntime(19467):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
03-04 01:25:23.293: E/AndroidRuntime(19467):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2100)
03-04 01:25:23.293: E/AndroidRuntime(19467):    ... 11 more
03-04 01:25:23.293: E/AndroidRuntime(19467): Caused by: java.lang.IllegalStateException: The meta-data tag in your app's AndroidManifest.xml does not have the right value.  Expected 4242000 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" />
03-04 01:25:23.293: E/AndroidRuntime(19467):    at com.google.android.gms.common.GooglePlayServicesUtil.n(Unknown Source)
03-04 01:25:23.293: E/AndroidRuntime(19467):    at com.google.android.gms.common.GooglePlayServicesUtil.isGooglePlayServicesAvailable(Unknown Source)
03-04 01:25:23.293: E/AndroidRuntime(19467):    at com.google.android.gms.maps.internal.q.v(Unknown Source)
03-04 01:25:23.293: E/AndroidRuntime(19467):    at com.google.android.gms.maps.internal.q.u(Unknown Source)
03-04 01:25:23.293: E/AndroidRuntime(19467):    at com.google.android.gms.maps.MapsInitializer.initialize(Unknown Source)
03-04 01:25:23.293: E/AndroidRuntime(19467):    at com.google.android.gms.maps.MapFragment$b.ex(Unknown Source)
03-04 01:25:23.293: E/AndroidRuntime(19467):    at com.google.android.gms.maps.MapFragment$b.a(Unknown Source)
03-04 01:25:23.293: E/AndroidRuntime(19467):    at com.google.android.gms.dynamic.a.a(Unknown Source)
03-04 01:25:23.293: E/AndroidRuntime(19467):    at com.google.android.gms.dynamic.a.onInflate(Unknown Source)
03-04 01:25:23.293: E/AndroidRuntime(19467):    at com.google.android.gms.maps.MapFragment.onInflate(Unknown Source)
03-04 01:25:23.293: E/AndroidRuntime(19467):    at android.app.Activity.onCreateView(Activity.java:4713)
03-04 01:25:23.293: E/AndroidRuntime(19467):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
03-04 01:25:23.293: E/AndroidRuntime(19467):    ... 22 more

main.xml

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <fragment
            android:id="@+id/map"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            class="com.google.android.gms.maps.MapFragment"
            android:layout_marginBottom="35dip" />
    </LinearLayout>
</RelativeLayout>

MainActivity.Java

public class MainActivity extends FragmentActivity {
private GoogleMap googleMap;
protected LocationListener locationListener;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    initializeMap();
}

@Override
public void onBackPressed() {

    finish();
}

private void initializeMap(){

    if(googleMap == null){

        googleMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
                .getMap();
        // check if map is created successfully or not
        if (googleMap == null) {
            Toast.makeText(getApplicationContext(),
                    "Sorry! unable to create maps", Toast.LENGTH_SHORT)
                    .show();
            }
        }
    }
}    

Manifest.xml

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

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

    <permission
        android:name="com.example.mapdemo.permission.MAPS_RECEIVE"
        android:protectionLevel="signature" />

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

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.appscourt.jooging.track.map.health.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

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

</manifest>

回答1:

Your logcat clearly said

 03-04 01:25:23.293: E/AndroidRuntime(19467): Caused by:
 java.lang.IllegalStateException: The meta-data tag in your app's
 AndroidManifest.xml does not have the right value.  Expected 4242000
 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" /> 03-04
 01:25:23.293: E/AndroidRuntime(19467):

Add the Google Play services version to your app's manifest

Edit your application's AndroidManifest.xml file, and add the following declaration within the element. This embeds the version of Google Play services that the app was compiled with.

You need to add <meta-data> under <application> tag into your AndroidManifest.xml

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

This is because latest google play services requires a version name, which is to be mentioned using <meta-data .. /> inside AndroidManifest.xml

and also change MainActivity extends Activity

For more information go to :https://developers.google.com/maps/documentation/android



回答2:

if you are using android-support-v4.jar (for backward support) then try SupportMapFragment

replace this line class="com.google.android.gms.maps.MapFragment"

with class="com.google.android.gms.maps.SupportMapFragment"

and also put in manifest.xml

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


回答3:

I think your package name and the permission tag package names are differing I think and even this android:name="com.appscourt.jooging.track.map.health.MainActivity" try to verify