請記住,我已經通過了數以百計的像這樣類似的問題看,但我似乎劃掉已在說明書上註明所有的檢查清單如何讓Google地圖開始工作。第一谷歌地圖API第2版的Android Studio的地圖不加載
第一件事,我會告訴我的代碼,然後我會講述正在發生的事情。
的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);
}
}
當我運行我的應用程序,我看到在左下角的谷歌徽標一張空白的畫布。在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密鑰位於清單文件的標記內。 - 確保谷歌地圖API v2 for Android在Google控制檯上啓用 - 我通過Gradle使用Google Play服務(向下滾動查看我的gradle片段) - 我確保設置了所有必要權限 - 我確保啓用glEsVersion。
我覺得這個問題是什麼?
我覺得我有我的「free.elephant.tuskapp」的味道有問題,Google的服務器進行身份驗證。在我的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」包名稱,而不是「免費獲得我的API .elephant.tuskapp」
這裏是我的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:[email protected]') {
transitive = true;
}
}
上午我就會錯過了什麼?或者它確實與我的味道有關?如果是口味造成麻煩,我該如何處理?我爲我的所有口味生成了新的api密鑰,但是我沒有看到我應該如何在清單文件中完成這項工作,並且看起來很優雅。
感謝您的答覆。我試了一下,但它崩潰了我的應用程序。所以我嘗試了一個不同的例子,但類似,現在應用程序顯示谷歌徽標在最底部,但沒有地圖加載。這是一張空白的地圖畫布:/。來自Logcat的完全沒有錯誤信息。 –
因此,對於灰色地圖,這意味着你的api-key有問題,請按照[this](https://www.youtube.com/watch?v=8ADb5bNOFHE),最後可以顯示地圖。 – bjiang
好吧,我將視頻追蹤到最後,我已經設置了一切,最後他指出了我遇到的一個常見錯誤。原來,我的api正在使用我的free.elephant.driverapp風格進行驗證。我是否允許支持多種口味的apikey? –