Java-Google Maps v2简单应用程序无法正常工作
作者:互联网
我正在关注this教程.这是我所有相关的文件:
activity_main.xml中
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
MainActivity.java
package org.barisakkurt.googlemapsv2;
import android.app.Fragment;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.widget.Toast;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
public class MainActivity extends FragmentActivity {
// Google Map
private GoogleMap googleMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
// Loading map
initilizeMap();
} catch (Exception e) {
}
}
/**
* function to load map. If map is not created it will create it for you
* */
private void initilizeMap() {
if (googleMap == null) {
googleMap = ((SupportMapFragment) getSupportFragmentManager().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();
}
}
}
@Override
protected void onResume() {
super.onResume();
initilizeMap();
}
}
AndroidManifest.xml中
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.barisakkurt.googlemapsv2"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<!-- Required to show current location -->
<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="org.barisakkurt.googlemapsv2.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="AIzaSyAC7y4bP-MjeJljm6Fr9Og1FKxNe9ZiFkY" />
</application>
</manifest>
我完成了教程,直到标题为“放置标记”为止.但是我收到以下错误.
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" />
如果我将清单文件写入以下行:
<meta-data
android:name="com.google.android.gms.version"
android:value="4030500" />
这次我得到这个错误:
Caused by: java.lang.RuntimeException: API key not found. Check that <meta-data android:name="com.google.android.maps.v2.API_KEY" android:value="your API key"/> is in the <application> element of AndroidManifest.xml
android在跟我开玩笑吗?为什么代码崩溃?我该怎么办才能运行该死的简单代码?为什么它在本教程中而不在我的机器上可以工作.这让我很烦.
(顺便说一句,我将Windows 7与实际设备Nexus 7结合使用,并且我复制了version.xml并尝试了以下元数据
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
再次没有工作.最后,我当然搜索了SO,谷歌看了流行的教程)请帮助我.
解决方法:
您需要具有两个元数据字段.一般而言,其中一项是Google Play服务所必需的,第二项是使用地图所必需的.
您应该在[android-sdk] /extras/google/google_play_services/samples/maps/AndroidManifest.xml中的示例应用程序中的示例代码段
<application
android:icon="@drawable/ic_launcher"
android:label="@string/demo_title"
android:hardwareAccelerated="true">
<!-- ** You need to replace the key below with your own key. **
The example key below will not be accepted because it is not linked to the
certificate which you will use to sign this application.
See: https://developers.google.com/maps/documentation/android/start
for instructions on how to get your own key. -->
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyCvGBr5in13NK2yYBR7lhXTtnxj3mrXQy4" />
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
标签:google-maps-api-2,android,java,google-maps,android-manifest 来源: https://codeday.me/bug/20191014/1912146.html