Android Studio:GoogleMaps无法加载

时间:2014-01-06 10:18:42

标签: java android android-studio google-maps-api-2

我已经尝试添加我的Android应用程序,我开发了Android Studio 0.4.0,一个带有API v2的GoogleMap。 我已经使用Sdk-Manager下载了GooglePlayServices包。 但是,如果我在模拟器中启动应用程序,它会崩溃:

 01-06 05:07:25.214    1137-1137/com.abc.example E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.abc.example, PID: 1137
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.abc.example/com.abc.example.MainActivity}: android.view.InflateException: Binary XML file line #2: Error inflating class fragment
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
            at android.app.ActivityThread.access$700(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4998)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class fragment
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:469)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
            at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290)
            at android.app.Activity.setContentView(Activity.java:1928)
            at com.gpspointsorg.gpspoints.MainActivity.onCreate(MainActivity.java:19)
            at android.app.Activity.performCreate(Activity.java:5243)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
            at android.app.ActivityThread.access$700(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4998)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.IllegalStateException: The meta-data tag in your app's AndroidManifest.xml does not have the right value.  Expected 4030500 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" />
            at com.google.android.gms.common.GooglePlayServicesUtil.n(Unknown Source)
            at com.google.android.gms.common.GooglePlayServicesUtil.isGooglePlayServicesAvailable(Unknown Source)
            at com.google.android.gms.maps.internal.q.v(Unknown Source)
            at com.google.android.gms.maps.internal.q.u(Unknown Source)
            at com.google.android.gms.maps.MapsInitializer.initialize(Unknown Source)
            at com.google.android.gms.maps.MapFragment$b.cE(Unknown Source)
            at com.google.android.gms.maps.MapFragment$b.a(Unknown Source)
            at com.google.android.gms.dynamic.a.a(Unknown Source)
            at com.google.android.gms.dynamic.a.onInflate(Unknown Source)
            at com.google.android.gms.maps.MapFragment.onInflate(Unknown Source)
            at android.app.Activity.onCreateView(Activity.java:4784)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:689)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:469)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
            at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290)
            at android.app.Activity.setContentView(Activity.java:1928)
            at com.gpspointsorg.gpspoints.MainActivity.onCreate(MainActivity.java:19)
            at android.app.Activity.performCreate(Activity.java:5243)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
            at android.app.ActivityThread.access$700(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4998)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
            at dalvik.system.NativeStart.main(Native Method)

这是MainActivity.java

package com.abc.example;

import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;

public class MainActivity extends Activity {

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


    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }


}

我的actvity_main.xml

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

AndroidManifest.xml

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

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

    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
    <!-- The following two permissions are not required to use
         Google Maps Android API v2, but are recommended. -->
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

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

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.abc.example.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="[Here is my Api key]"/>

    </application>

</manifest>

这里是build.gradle:

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.7.+'
    }
}
apply plugin: 'android'

repositories {
    mavenCentral()
}

android {
    compileSdkVersion 19
    buildToolsVersion "19.0.0"

    defaultConfig {
        minSdkVersion 14
        targetSdkVersion 19
    }
    buildTypes {
        release {
            runProguard false
            proguardFile getDefaultProguardFile('proguard-android.txt')
        }
    }
    productFlavors {
        defaultFlavor {
            proguardFile 'proguard-rules.txt'
        }
    }
}

dependencies {
    compile 'com.android.support:appcompat-v7:+'
    compile 'com.google.android.gms:play-services:4.0.30'
}

很抱歉我的问题很长,但也许你有任何想法? 提前谢谢。

3 个答案:

答案 0 :(得分:0)

在清单文件的application标记中移动以下内容

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

确保您拥有更新的Google Play服务。

答案 1 :(得分:0)

您在<application>标记之外添加了以下代码,这就是它抛出错误的原因:

  

您应用的AndroidManifest.xml中的元数据标记没有正确的值。预计4030500但找到0.您必须在其中包含以下声明          元素:........

<application>标记内的清单文件中添加以下行。

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

查看Documentation已经声明仅在meta-data标记内写<application>标记的{{3}}。

答案 2 :(得分:0)

对我来说工作... MI 4地图页面不工作问题是片段膨胀异常。添加后,我解决了所有问题。

<activity android:name=".locApp.modules.home.views.MapAndPlacesActivity">
<intent-filter>
<action android:name="android.intent.action.VIEW" /> 
</intent-filter> 
</activity>