Google Maps API v2 Android Studio Map未加载

时间:2015-06-22 14:44:54

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

请记住,我已经阅读了数百个类似的问题,但是我似乎已经完成了关于如何让Google地图开始工作的说明中所列的所有内容的清单

首先,我会展示我的代码,然后我会讲述发生的事情。

的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="elephant.tuskapp"
      android:versionCode="49"
      android:versionName="4.6">

     <uses-sdk android:minSdkVersion="15"/>
     <uses-feature
         android:glEsVersion="0x00020000"
         android:required="true"/>
     <permission android:name="elephant.tuskapp.permission.MAPS_RECEIVE"
    android:protectionLevel="signature"/>
     <uses-permission android:name="elephant.tuskapp.permission.MAPS_RECEIVE"/>
     <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.ACCESS_GPS"/>
     <uses-permission android:name="android.permission.ACCESS_LOCATION"/>
     <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
     <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
     <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
     <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
     <uses-permission android:name="android.permission.CALL_PHONE"/>
     <uses-permission android:name="android.permission.WAKE_LOCK"/>
     <uses-permission android:name="android.permission.SEND_SMS"/>
     <uses-permission android:name="android.permission.READ_PHONE_STATE"/>
     <uses-permission android:name="android.permission.CAMERA"/>
     <uses-feature android:name="android.hardware.camera"/>
     <uses-feature android:name="android.hardware.camera.autofocus"/>

     <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE"/>
     <uses-permission android:name="android.permission.GET_ACCOUNTS"/>

     <permission android:name="elephant.tuskapp.permission.C2D_MESSAGE" android:protectionLevel="signature"/>
     <uses-permission android:name="elephant.tuskapp.permission.C2D_MESSAGE"/>

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

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

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

       <activity
             android:name=".activity.MainActivity"
             android:label="@string/activity_name">
            <intent-filter>
                 <action android:name="android.intent.action.MAIN"/>
                 <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
         </activity>

         <receiver android:name="elephant.tuskapp.service.AlarmReceiver"></receiver>

    <receiver android:name="elephant.tuskapp.service.TimeSynchronizer">
         <intent-filter>
              <action android:name="android.intent.action.ACTION_TIME_CHANGED"/>
              <action android:name="android.intent.action.TIMEZONE_CHANGED"/>
        </intent-filter>
    </receiver>

    <service android:name=".service.GPSService"></service>
    <service android:name=".service.RouteUpdateService"></service>

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

    <receiver
            android:name=".service.GcmBroadcastReceiver"
            android:permission="com.google.android.c2dm.permission.SEND">
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE"/>
            <category android:name="elephant.tuskapp"/>
        </intent-filter>
    </receiver>
    <service android:name=".service.GcmIntentService"/>

    <meta-data
        android:name="io.fabric.ApiKey"
        android:value="key" />

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

布局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"/>

主要活动

package elephant.tuskapp.activity;
import android.app.Activity;
import android.os.Bundle;
import elephant.tuskapp.R;

public class MainActivity extends Activity {

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

当我运行我的应用时,我会在左下角看到一个带有Google徽标的空白画布。 Logcat打印出以下错误:

-22 20:47:24.822  22720-22720/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: free.elephant.tuskapp, PID: 22720
java.lang.RuntimeException: Unable to start activity ComponentInfo{free.elephant.tuskapp/elephant.tuskapp.activity.MainActivity}: android.view.InflateException: Binary XML file line #2: Error inflating class fragment
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2301)
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2361)
      at android.app.ActivityThread.access$800(ActivityThread.java:147)
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1281)
      at android.os.Handler.dispatchMessage(Handler.java:102)
      at android.os.Looper.loop(Looper.java:135)
      at android.app.ActivityThread.main(ActivityThread.java:5232)
      at java.lang.reflect.Method.invoke(Native Method)
      at java.lang.reflect.Method.invoke(Method.java:372)
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)
Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class fragment
      at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:767)
      at android.view.LayoutInflater.inflate(LayoutInflater.java:486)
      at android.view.LayoutInflater.inflate(LayoutInflater.java:418)
      at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
      at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:377)
      at android.app.Activity.setContentView(Activity.java:2161)
      at elephant.tuskapp.activity.MainActivity.onCreate(MainActivity.java:18)
      at android.app.Activity.performCreate(Activity.java:5984)
      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2254)
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2361)
      at android.app.ActivityThread.access$800(ActivityThread.java:147)
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1281)
      at android.os.Handler.dispatchMessage(Handler.java:102)
      at android.os.Looper.loop(Looper.java:135)
      at android.app.ActivityThread.main(ActivityThread.java:5232)
      at java.lang.reflect.Method.invoke(Native Method)
      at java.lang.reflect.Method.invoke(Method.java:372)
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)
Caused by: java.lang.RuntimeException: API key not found.  Check that <meta-data android:name="com.google.android.geo.API_KEY" android:value="your API key"/> is in the <application> element of AndroidManifest.xml
      at com.google.maps.api.android.lib6.d.fk.a(Unknown Source)
      at com.google.maps.api.android.lib6.a.g.a(Unknown Source)
      at com.google.android.gms.maps.internal.CreatorImpl.b(Unknown Source)
      at com.google.android.gms.maps.internal.CreatorImpl.b(Unknown Source)
      at com.google.android.gms.maps.internal.i.onTransact(SourceFile:62)
      at android.os.Binder.transact(Binder.java:380)
      at com.google.android.gms.maps.internal.zzc$zza$zza.zzr(Unknown Source)
      at com.google.android.gms.maps.MapFragment$zzb.zzvu(Unknown Source)
      at com.google.android.gms.maps.MapFragment$zzb.zza(Unknown Source)
      at com.google.android.gms.dynamic.zza.zza(Unknown Source)
      at com.google.android.gms.dynamic.zza.onInflate(Unknown Source)
      at com.google.android.gms.maps.MapFragment.onInflate(Unknown Source)
      at android.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2117)
      at android.app.Activity.onCreateView(Activity.java:5333)
      at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:737)
      at android.view.LayoutInflater.inflate(LayoutInflater.java:486)
      at android.view.LayoutInflater.inflate(LayoutInflater.java:418)
      at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
      at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:377)
      at android.app.Activity.setContentView(Activity.java:2161)
      at elephant.tuskapp.activity.MainActivity.onCreate(MainActivity.java:18)
      at android.app.Activity.performCreate(Activity.java:5984)
      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2254)
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2361)
      at android.app.ActivityThread.access$800(ActivityThread.java:147)
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1281)
      at android.os.Handler.dispatchMessage(Handler.java:102)
      at android.os.Looper.loop(Looper.java:135)
      at android.app.ActivityThread.main(ActivityThread.java:5232)
      at java.lang.reflect.Method.invoke(Native Method)
      at java.lang.reflect.Method.invoke(Method.java:372)
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)

熟悉吧?你在很多其他问题中看到它。我认为我的问题与其他问题的不同之处在于我的应用程序可能存在不同风格的问题。

为了消除其他人遇到的错误,我做了以下事情:   - 确保我的api密钥位于清单文件的标记内。   - 确保在Google控制台上启用了适用于Android的Google Maps API v2   - 我正在通过Gradle使用Google Play服务(向下滚动我的gradle代码段)   - 我确保设置了所有基本权限   - 我确保启用glEsVersion。

我认为问题是什么?

我认为我的“free.elephant.tuskapp”风格存在问题,需要使用googles服务器进行身份验证。在我的gradle构建中,我维护了两种不同的应用程序,我似乎在验证时遇到错误:

Ensure that the "Google Maps Android API v2" is enabled.
Ensure that the following Android Key exists:
API Key: myAPIKEY
Android Application (<cert_fingerprint>;<package_name>): SHA-1;free.elephant.tuskapp

我使用'elephant.tuskapp'软件包名称而不是'free.elephant.tuskapp'

获取了我的API

如果有任何帮助,这是我的gradle构建: 的摇篮

   buildscript {
        repositories {
            maven { 
                url 'https://maven.fabric.io/public' 
            }
        }

        dependencies {
            classpath 'io.fabric.tools:gradle:1.+'
        }
    }
    apply plugin: 'com.android.application'
    apply plugin: 'io.fabric'

    repositories {
        maven { 
            url 'https://maven.fabric.io/public' 
        }
    }


    android {
        compileSdkVersion 22
        buildToolsVersion "23.0.0 rc2"

        defaultConfig {
            applicationId "elephant.tuskapp"
            minSdkVersion 15
            targetSdkVersion 15
        }

        productFlavors {
            demo {
                applicationId "demo.elephant.tuskapp"
            }
            free {
                applicationId "free.elephant.tuskapp"
            }
        }

        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
            }
        }
    }

    dependencies {
        compile 'com.google.android.gms:play-services:7.5.0'
        compile 'com.google.zxing:core:3.2.0'
        compile('com.crashlytics.sdk.android:crashlytics:2.3.2@aar') {
            transitive = true;
        }
    }

我错过了什么吗?或者它真的与我的味道构建有关吗?如果这是引起麻烦的味道,我该如何应对呢?我为我的所有口味生成了新的api密钥,但是我没有看到我应该如何在清单文件中使其工作以及让它看起来更优雅。

2 个答案:

答案 0 :(得分:2)

根据您的logcat,它说明您的地图片段布局有问题。另外,我发现MainActivity中没有任何内容,我认为你应该在那里初步绘制地图。有关详细信息,请参阅代码here

示例代码:

public class MainActivity extends FragmentActivity {

    GoogleMap mGoogleMap;
    private static LatLng MountainView = new LatLng(37.42, -122.08);

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

        // Initial Map
        try {

            if (mGoogleMap == null) {
                mGoogleMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        mGoogleMap.getUiSettings().setMyLocationButtonEnabled(true);
        mGoogleMap.getUiSettings().setZoomControlsEnabled(true);
        mGoogleMap.getUiSettings().setZoomGesturesEnabled(true);

        mGoogleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(MountainView, 15));

        Marker marker = mGoogleMap.addMarker(new MarkerOptions()
                .position(MountainView)
                .title("Have a nice day!"));

    }
}

答案 1 :(得分:0)

由于我的应用程序有多种口味,我被抛出一个错误,我的api-key没有被验证。解决此问题的方法是确保将项目的所有包添加到Google Developer Console中。

SHA1;包名

然后按enter

SHA1; packagename2(味)

就我而言:

SHA1;elephant.tuskapp
SHA1;free.elephant.tuskapp
SHA1;demo.elephant.tuskapp