【发布时间】:2017-05-29 05:57:45
【问题描述】:
我不断收到此错误:
未找到 API 密钥。检查
当我的地图活动试图膨胀活动的片段时。
有人遇到过这个问题吗?
在我的字符串文件中:
<string name="google_api_key" translatable="false" templateMergeStrategy="preserve">an api key that i got from google here</string>
我试过了
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="@string/google_api_key"/>
并直接在字段中键入密钥:
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="an api key that i got from google here"/>
我也试过了:
- 清理缓存
- 使缓存失效并重新启动
- 通过 XML 检查器运行我的所有 XML 文件以进行验证。
MapActivity.java
public class MapActivity extends AppCompatActivity {
private static final String MAP_COORDS = "com.mycompany.myapp.map_coordinates";
public static final int MAP_REQUEST_CODE = 453;
private MyObject myObject;
public static Intent newIntent(Context packageContext, MyObject myObject) {
Intent intent = new Intent(packageContext, MapActivity.class);
Bundle bundle = new Bundle();
bundle.putParcelable(MAP_COORDS, myObject);
intent.putExtras(bundle);
return intent;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map);
}
}
MapActivity.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"
tools:context="com.mycompany.myapp.Views.MasterDetails.MapActivity">
<fragment
android:id="@+id/map"
android:name="com.mycompany.myapp.Views.MasterDetails.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
在我的 Gradle 依赖项中
compile 'com.google.android.gms:play-services-maps:7.8.0'
compile 'com.google.android.gms:play-services-location:7.8.0'
AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version"/>
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="@string/google_api_key"/>
【问题讨论】:
-
相对于
<application>元素,那些<meta-data>元素在哪里? -
Application 元素内部,Activity 声明的正上方。
标签: java android google-maps android-manifest