我正在尝试使用本教程:http://www.tutorialspoint.com/android/android_google_maps.htm
这是代码:
public class ViewMapActivity extends FragmentActivity {
static final LatLng TutorialsPoint = new LatLng(21 , 57);
private GoogleMap googleMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
//super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_map);
try {
if (googleMap == null) {
googleMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
}
googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
Marker TP = googleMap.addMarker(new MarkerOptions().position(TutorialsPoint).title("TutorialsPoint"));
} catch (Exception e) {
e.printStackTrace();
}
}
}
布局:
<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"
android:background="#0099cc"
tools:context="com.androidpoiservice.ViewMapActivity" >
<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.androidpoiservice"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="com.example.googlemaps.permission.MAPS_RECEIVE" />
<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="16" />
<permission
android:name="com.example.googlemaps.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.androidpoiservice.StartUpActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="@string/title_activity_start_up"
android:theme="@style/FullscreenTheme" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.androidpoiservice.LoginActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="@string/title_activity_login"
android:theme="@style/FullscreenTheme" >
</activity>
<activity
android:name="com.androidpoiservice.RegisterActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="@string/title_activity_register"
android:theme="@style/FullscreenTheme" >
</activity>
<activity
android:name="com.androidpoiservice.MainMenuActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="@string/title_activity_main_menu"
android:theme="@style/FullscreenTheme" >
</activity>
<activity
android:name="com.androidpoiservice.AddPOIActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="@string/title_activity_add_poi"
android:theme="@style/FullscreenTheme" >
</activity>
<activity
android:name="com.androidpoiservice.ViewMapActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="@string/title_activity_view_map"
android:theme="@style/FullscreenTheme" >
</activity>
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyAgjXQt9zYFw5m0t7BIhxGT6e82mnZQUEA" />
</application>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<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" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-feature android:glEsVersion="0x00020000" android:required="true" />
这是我得到的错误:
03-13 20:21:23.560: E/AndroidRuntime(1130): FATAL EXCEPTION: main
03-13 20:21:23.560: E/AndroidRuntime(1130): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.androidpoiservice/com.androidpoiservice.ViewMapActivity}: android.view.InflateException: Binary XML file line #8: Error inflating class fragment
03-13 20:21:23.560: E/AndroidRuntime(1130): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
03-13 20:21:23.560: E/AndroidRuntime(1130): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
03-13 20:21:23.560: E/AndroidRuntime(1130): at android.app.ActivityThread.access$600(ActivityThread.java:130)
03-13 20:21:23.560: E/AndroidRuntime(1130): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
03-13 20:21:23.560: E/AndroidRuntime(1130): at android.os.Handler.dispatchMessage(Handler.java:99)
03-13 20:21:23.560: E/AndroidRuntime(1130): at android.os.Looper.loop(Looper.java:137)
03-13 20:21:23.560: E/AndroidRuntime(1130): at android.app.ActivityThread.main(ActivityThread.java:4745)
03-13 20:21:23.560: E/AndroidRuntime(1130): at java.lang.reflect.Method.invokeNative(Native Method)
03-13 20:21:23.560: E/AndroidRuntime(1130): at java.lang.reflect.Method.invoke(Method.java:511)
03-13 20:21:23.560: E/AndroidRuntime(1130): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
03-13 20:21:23.560: E/AndroidRuntime(1130): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
03-13 20:21:23.560: E/AndroidRuntime(1130): at dalvik.system.NativeStart.main(Native Method)
03-13 20:21:23.560: E/AndroidRuntime(1130): Caused by: android.view.InflateException: Binary XML file line #8: Error inflating class fragment
03-13 20:21:23.560: E/AndroidRuntime(1130): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
03-13 20:21:23.560: E/AndroidRuntime(1130): at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
03-13 20:21:23.560: E/AndroidRuntime(1130): at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
03-13 20:21:23.560: E/AndroidRuntime(1130): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
03-13 20:21:23.560: E/AndroidRuntime(1130): at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
03-13 20:21:23.560: E/AndroidRuntime(1130): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:256)
03-13 20:21:23.560: E/AndroidRuntime(1130): at android.app.Activity.setContentView(Activity.java:1867)
03-13 20:21:23.560: E/AndroidRuntime(1130): at com.androidpoiservice.ViewMapActivity.onCreate(ViewMapActivity.java:32)
03-13 20:21:23.560: E/AndroidRuntime(1130): at android.app.Activity.performCreate(Activity.java:5008)
03-13 20:21:23.560: E/AndroidRuntime(1130): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
03-13 20:21:23.560: E/AndroidRuntime(1130): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
03-13 20:21:23.560: E/AndroidRuntime(1130): ... 11 more
03-13 20:21:23.560: E/AndroidRuntime(1130): Caused by: java.lang.IllegalStateException: The meta-data tag in your app's AndroidManifest.xml does not have the right value. Expected 4242000 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" />
03-13 20:21:23.560: E/AndroidRuntime(1130): at com.google.android.gms.common.GooglePlayServicesUtil.n(Unknown Source)
03-13 20:21:23.560: E/AndroidRuntime(1130): at com.google.android.gms.common.GooglePlayServicesUtil.isGooglePlayServicesAvailable(Unknown Source)
03-13 20:21:23.560: E/AndroidRuntime(1130): at com.google.android.gms.maps.internal.q.v(Unknown Source)
03-13 20:21:23.560: E/AndroidRuntime(1130): at com.google.android.gms.maps.internal.q.u(Unknown Source)
03-13 20:21:23.560: E/AndroidRuntime(1130): at com.google.android.gms.maps.MapsInitializer.initialize(Unknown Source)
03-13 20:21:23.560: E/AndroidRuntime(1130): at com.google.android.gms.maps.MapFragment$b.ex(Unknown Source)
03-13 20:21:23.560: E/AndroidRuntime(1130): at com.google.android.gms.maps.MapFragment$b.a(Unknown Source)
03-13 20:21:23.560: E/AndroidRuntime(1130): at com.google.android.gms.dynamic.a.a(Unknown Source)
03-13 20:21:23.560: E/AndroidRuntime(1130): at com.google.android.gms.dynamic.a.onInflate(Unknown Source)
03-13 20:21:23.560: E/AndroidRuntime(1130): at com.google.android.gms.maps.MapFragment.onInflate(Unknown Source)
03-13 20:21:23.560: E/AndroidRuntime(1130): at android.app.Activity.onCreateView(Activity.java:4663)
03-13 20:21:23.560: E/AndroidRuntime(1130): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
03-13 20:21:23.560: E/AndroidRuntime(1130): ... 21 more
答案 0 :(得分:2)
将此项添加为元标记作为应用程序标记的子项
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name">
<activity
...// rest of the code
</activity>
<meta-data android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<!-- Goolge API Key -->
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="api key" />
</application>
更改此
public class ViewMapActivity extends FragmentActivity
到
public class ViewMapActivity extends Activity
编辑:
取消注释
//super.onCreate(savedInstanceState);