我按照https://developers.google.com/maps/documentation/android/start中的教程,我发现对我来说没有正确答案。
这是我的mainactivity.java,我的MainActivity扩展了FragmentActivity:
package com.example.googlemaptest;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
public class MainActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
和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"/>
清单如下:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.googlemaptest"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<permission
android:name="com.example.googlemaptest.permission.MAPS_RECEIVE"
android:protectionLevel="signature"/>
<uses-feature android:glEsVersion="0x00020000" android:required="true"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
<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" >
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version"
/>
<meta-data
android:name="com.google.maps.v2.API_KEY"
android:value="AIzaSyB_wRz081KuDphomkrGUNaweTVB843Tt2s"
/>
<activity
android:name="com.example.googlemaptest.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>
</application>
</manifest>
以下是错误:
12-07 19:47:12.452: E/AndroidRuntime(9568): FATAL EXCEPTION: main
12-07 19:47:12.452: E/AndroidRuntime(9568): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.googlemaptest/com.example.googlemaptest.MainActivity}: android.view.InflateException: Binary XML file line #2: Error inflating class fragment
12-07 19:47:12.452: E/AndroidRuntime(9568): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1849)
12-07 19:47:12.452: E/AndroidRuntime(9568): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1870)
12-07 19:47:12.452: E/AndroidRuntime(9568): at android.app.ActivityThread.access$1500(ActivityThread.java:132)
12-07 19:47:12.452: E/AndroidRuntime(9568): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1051)
12-07 19:47:12.452: E/AndroidRuntime(9568): at android.os.Handler.dispatchMessage(Handler.java:99)
12-07 19:47:12.452: E/AndroidRuntime(9568): at android.os.Looper.loop(Looper.java:150)
12-07 19:47:12.452: E/AndroidRuntime(9568): at android.app.ActivityThread.main(ActivityThread.java:4319)
12-07 19:47:12.452: E/AndroidRuntime(9568): at java.lang.reflect.Method.invokeNative(Native Method)
12-07 19:47:12.452: E/AndroidRuntime(9568): at java.lang.reflect.Method.invoke(Method.java:507)
12-07 19:47:12.452: E/AndroidRuntime(9568): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:849)
12-07 19:47:12.452: E/AndroidRuntime(9568): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607)
12-07 19:47:12.452: E/AndroidRuntime(9568): at dalvik.system.NativeStart.main(Native Method)
12-07 19:47:12.452: E/AndroidRuntime(9568): Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class fragment
12-07 19:47:12.452: E/AndroidRuntime(9568): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:581)
12-07 19:47:12.452: E/AndroidRuntime(9568): at android.view.LayoutInflater.inflate(LayoutInflater.java:386)
12-07 19:47:12.452: E/AndroidRuntime(9568): at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
12-07 19:47:12.452: E/AndroidRuntime(9568): at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
12-07 19:47:12.452: E/AndroidRuntime(9568): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:250)
12-07 19:47:12.452: E/AndroidRuntime(9568): at android.app.Activity.setContentView(Activity.java:1742)
12-07 19:47:12.452: E/AndroidRuntime(9568): at com.example.googlemaptest.MainActivity.onCreate(MainActivity.java:10)
12-07 19:47:12.452: E/AndroidRuntime(9568): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1072)
12-07 19:47:12.452: E/AndroidRuntime(9568): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1813)
12-07 19:47:12.452: E/AndroidRuntime(9568): ... 11 more
12-07 19:47:12.452: E/AndroidRuntime(9568): Caused by: java.lang.ClassNotFoundException: android.view.fragment in loader dalvik.system.PathClassLoader[/data/app/com.example.googlemaptest-2.apk]
12-07 19:47:12.452: E/AndroidRuntime(9568): at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240)
12-07 19:47:12.452: E/AndroidRuntime(9568): at java.lang.ClassLoader.loadClass(ClassLoader.java:551)
12-07 19:47:12.452: E/AndroidRuntime(9568): at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
12-07 19:47:12.452: E/AndroidRuntime(9568): at android.view.LayoutInflater.createView(LayoutInflater.java:471)
12-07 19:47:12.452: E/AndroidRuntime(9568): at android.view.LayoutInflater.onCreateView(LayoutInflater.java:549)
12-07 19:47:12.452: E/AndroidRuntime(9568): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:66)
12-07 19:47:12.452: E/AndroidRuntime(9568): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:568)
12-07 19:47:12.452: E/AndroidRuntime(9568): ... 19 more
答案 0 :(得分:0)
使用supportMapFragment而不是MapFragment。
<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
/>
GoogleMap gm;
//oncreate
setContentView(R.layout.maps)
gm = ((SupportMapFragment)(getSupportFragmentManager().findFragmentById(R.id.map))).getMap();
gm.setMapType(GoogleMap.MAP_TYPE_NORMAL);
gm.setMyLocationEnabled(true);
}