public class MainActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
清单文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.map.googlemapv2"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<permission
android:name="com.map.googlemapv2.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<!--
The following two permissions are not required to use
Google Maps Android API v2, but are recommended.
-->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="com.map.googlemapv2.permission.MAPS_RECEIVE" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.map.googlemapv2.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="AIzaSyXXXXXXXXXXXXXXXXXXXXXXXX" />
</application>
</manifest>
Layout File
<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=".MainActivity" >
<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/btnSurrey" />
</RelativeLayout>
StackTrace:
12-12 06:19:45.460: D/AndroidRuntime(907): Shutting down VM
12-12 06:19:45.460: W/dalvikvm(907): threadid=1: thread exiting with uncaught exception (group=0xb4aeab90)
12-12 06:19:45.500: E/AndroidRuntime(907): FATAL EXCEPTION: main
12-12 06:19:45.500: E/AndroidRuntime(907): Process: com.amaps.androidmap, PID: 907
12-12 06:19:45.500: E/AndroidRuntime(907): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.amaps.androidmap/com.amaps.androidmap.MainActivity}: android.view.InflateException: Binary XML file line #2: Error inflating class fragment
12-12 06:19:45.500: E/AndroidRuntime(907): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176)
12-12 06:19:45.500: E/AndroidRuntime(907): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
12-12 06:19:45.500: E/AndroidRuntime(907): at android.app.ActivityThread.access$700(ActivityThread.java:135)
12-12 06:19:45.500: E/AndroidRuntime(907): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
12-12 06:19:45.500: E/AndroidRuntime(907): at android.os.Handler.dispatchMessage(Handler.java:102)
12-12 06:19:45.500: E/AndroidRuntime(907): at android.os.Looper.loop(Looper.java:137)
12-12 06:19:45.500: E/AndroidRuntime(907): at android.app.ActivityThread.main(ActivityThread.java:4998)
12-12 06:19:45.500: E/AndroidRuntime(907): at java.lang.reflect.Method.invokeNative(Native Method)
12-12 06:19:45.500: E/AndroidRuntime(907): at java.lang.reflect.Method.invoke(Method.java:515)
12-12 06:19:45.500: E/AndroidRuntime(907): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
12-12 06:19:45.500: E/AndroidRuntime(907): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
12-12 06:19:45.500: E/AndroidRuntime(907): at dalvik.system.NativeStart.main(Native Method)
12-12 06:19:45.500: E/AndroidRuntime(907): Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class fragment
12-12 06:19:45.500: E/AndroidRuntime(907): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
12-12 06:19:45.500: E/AndroidRuntime(907): at android.view.LayoutInflater.inflate(LayoutInflater.java:469)
12-12 06:19:45.500: E/AndroidRuntime(907): at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
12-12 06:19:45.500: E/AndroidRuntime(907): at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
12-12 06:19:45.500: E/AndroidRuntime(907): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290)
12-12 06:19:45.500: E/AndroidRuntime(907): at android.app.Activity.setContentView(Activity.java:1928)
12-12 06:19:45.500: E/AndroidRuntime(907): at com.amaps.androidmap.MainActivity.onCreate(MainActivity.java:12)
12-12 06:19:45.500: E/AndroidRuntime(907): at android.app.Activity.performCreate(Activity.java:5243)
12-12 06:19:45.500: E/AndroidRuntime(907): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
12-12 06:19:45.500: E/AndroidRuntime(907): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
12-12 06:19:45.500: E/AndroidRuntime(907): ... 11 more
12-12 06:19:45.500: E/AndroidRuntime(907): Caused by: android.app.Fragment$InstantiationException: Unable to instantiate fragment com.google.android.gms.maps.MapFragment: make sure class name exists, is public, and has an empty constructor that is public
12-12 06:19:45.500: E/AndroidRuntime(907): at android.app.Fragment.instantiate(Fragment.java:597)
12-12 06:19:45.500: E/AndroidRuntime(907): at android.app.Fragment.instantiate(Fragment.java:561)
12-12 06:19:45.500: E/AndroidRuntime(907): at android.app.Activity.onCreateView(Activity.java:4777)
12-12 06:19:45.500: E/AndroidRuntime(907): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:689)
12-12 06:19:45.500: E/AndroidRuntime(907): ... 20 more
12-12 06:19:45.500: E/AndroidRuntime(907): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.google.android.gms.maps.MapFragment" on path: DexPathList[[zip file "/data/app/com.amaps.androidmap-2.apk"],nativeLibraryDirectories=[/data/app-lib/com.amaps.androidmap-2, /system/lib]]
12-12 06:19:45.500: E/AndroidRuntime(907): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
12-12 06:19:45.500: E/AndroidRuntime(907): at java.lang.ClassLoader.loadClass(ClassLoader.java:497)
12-12 06:19:45.500: E/AndroidRuntime(907): at java.lang.ClassLoader.loadClass(ClassLoader.java:457)
12-12 06:19:45.500: E/AndroidRuntime(907): at android.app.Fragment.instantiate(Fragment.java:583)
12-12 06:19:45.500: E/AndroidRuntime(907): ... 23 more
12-12 06:23:20.842: I/Process(907): Sending signal. PID: 907 SIG: 9
12-12 06:44:02.832: D/AndroidRuntime(1054): Shutting down VM
12-12 06:44:02.832: W/dalvikvm(1054): threadid=1: thread exiting with uncaught exception (group=0xb4aeab90)
12-12 06:44:02.852: E/AndroidRuntime(1054): FATAL EXCEPTION: main
12-12 06:44:02.852: E/AndroidRuntime(1054): Process: com.amaps.androidmap, PID: 1054
12-12 06:44:02.852: E/AndroidRuntime(1054): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.amaps.androidmap/com.amaps.androidmap.MainActivity}: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
12-12 06:44:02.852: E/AndroidRuntime(1054): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176)
12-12 06:44:02.852: E/AndroidRuntime(1054): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
12-12 06:44:02.852: E/AndroidRuntime(1054): at android.app.ActivityThread.access$700(ActivityThread.java:135)
12-12 06:44:02.852: E/AndroidRuntime(1054): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
12-12 06:44:02.852: E/AndroidRuntime(1054): at android.os.Handler.dispatchMessage(Handler.java:102)
12-12 06:44:02.852: E/AndroidRuntime(1054): at android.os.Looper.loop(Looper.java:137)
12-12 06:44:02.852: E/AndroidRuntime(1054): at android.app.ActivityThread.main(ActivityThread.java:4998)
12-12 06:44:02.852: E/AndroidRuntime(1054): at java.lang.reflect.Method.invokeNative(Native Method)
12-12 06:44:02.852: E/AndroidRuntime(1054): at java.lang.reflect.Method.invoke(Method.java:515)
12-12 06:44:02.852: E/AndroidRuntime(1054): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
12-12 06:44:02.852: E/AndroidRuntime(1054): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
12-12 06:44:02.852: E/AndroidRuntime(1054): at dalvik.system.NativeStart.main(Native Method)
12-12 06:44:02.852: E/AndroidRuntime(1054): Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
12-12 06:44:02.852: E/AndroidRuntime(1054): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
12-12 06:44:02.852: E/AndroidRuntime(1054): at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
12-12 06:44:02.852: E/AndroidRuntime(1054): at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
12-12 06:44:02.852: E/AndroidRuntime(1054): at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
12-12 06:44:02.852: E/AndroidRuntime(1054): at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
12-12 06:44:02.852: E/AndroidRuntime(1054): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290)
12-12 06:44:02.852: E/AndroidRuntime(1054): at android.app.Activity.setContentView(Activity.java:1928)
12-12 06:44:02.852: E/AndroidRuntime(1054): at com.amaps.androidmap.MainActivity.onCreate(MainActivity.java:12)
12-12 06:44:02.852: E/AndroidRuntime(1054): at android.app.Activity.performCreate(Activity.java:5243)
12-12 06:44:02.852: E/AndroidRuntime(1054): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
12-12 06:44:02.852: E/AndroidRuntime(1054): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
12-12 06:44:02.852: E/AndroidRuntime(1054): ... 11 more
12-12 06:44:02.852: E/AndroidRuntime(1054): Caused by: android.app.Fragment$InstantiationException: Unable to instantiate fragment com.google.android.gms.maps.SupportMapFragment: make sure class name exists, is public, and has an empty constructor that is public
12-12 06:44:02.852: E/AndroidRuntime(1054): at android.app.Fragment.instantiate(Fragment.java:597)
12-12 06:44:02.852: E/AndroidRuntime(1054): at android.app.Fragment.instantiate(Fragment.java:561)
12-12 06:44:02.852: E/AndroidRuntime(1054): at android.app.Activity.onCreateView(Activity.java:4777)
12-12 06:44:02.852: E/AndroidRuntime(1054): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:689)
12-12 06:44:02.852: E/AndroidRuntime(1054): ... 21 more
12-12 06:44:02.852: E/AndroidRuntime(1054): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.google.android.gms.maps.SupportMapFragment" on path: DexPathList[[zip file "/data/app/com.amaps.androidmap-1.apk"],nativeLibraryDirectories=[/data/app-lib/com.amaps.androidmap-1, /system/lib]]
12-12 06:44:02.852: E/AndroidRuntime(1054): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
12-12 06:44:02.852: E/AndroidRuntime(1054): at java.lang.ClassLoader.loadClass(ClassLoader.java:497)
12-12 06:44:02.852: E/AndroidRuntime(1054): at java.lang.ClassLoader.loadClass(ClassLoader.java:457)
12-12 06:44:02.852: E/AndroidRuntime(1054): at android.app.Fragment.instantiate(Fragment.java:583)
12-12 06:44:02.852: E/AndroidRuntime(1054): ... 24 more
12-12 06:49:03.212: I/Process(1054): Sending signal. PID: 1054 SIG: 9
12-12 06:58:53.742: D/AndroidRuntime(1100): Shutting down VM
12-12 06:58:53.742: W/dalvikvm(1100): threadid=1: thread exiting with uncaught exception (group=0xb4aeab90)
12-12 06:58:53.762: E/AndroidRuntime(1100): FATAL EXCEPTION: main
12-12 06:58:53.762: E/AndroidRuntime(1100): Process: com.amaps.androidmap, PID: 1100
12-12 06:58:53.762: E/AndroidRuntime(1100): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.amaps.androidmap/com.amaps.androidmap.MainActivity}: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
12-12 06:58:53.762: E/AndroidRuntime(1100): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176)
12-12 06:58:53.762: E/AndroidRuntime(1100): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
12-12 06:58:53.762: E/AndroidRuntime(1100): at android.app.ActivityThread.access$700(ActivityThread.java:135)
12-12 06:58:53.762: E/AndroidRuntime(1100): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
12-12 06:58:53.762: E/AndroidRuntime(1100): at android.os.Handler.dispatchMessage(Handler.java:102)
12-12 06:58:53.762: E/AndroidRuntime(1100): at android.os.Looper.loop(Looper.java:137)
12-12 06:58:53.762: E/AndroidRuntime(1100): at android.app.ActivityThread.main(ActivityThread.java:4998)
12-12 06:58:53.762: E/AndroidRuntime(1100): at java.lang.reflect.Method.invokeNative(Native Method)
12-12 06:58:53.762: E/AndroidRuntime(1100): at java.lang.reflect.Method.invoke(Method.java:515)
12-12 06:58:53.762: E/AndroidRuntime(1100): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
12-12 06:58:53.762: E/AndroidRuntime(1100): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
12-12 06:58:53.762: E/AndroidRuntime(1100): at dalvik.system.NativeStart.main(Native Method)
12-12 06:58:53.762: E/AndroidRuntime(1100): Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
12-12 06:58:53.762: E/AndroidRuntime(1100): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
12-12 06:58:53.762: E/AndroidRuntime(1100): at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
12-12 06:58:53.762: E/AndroidRuntime(1100): at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
12-12 06:58:53.762: E/AndroidRuntime(1100): at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
12-12 06:58:53.762: E/AndroidRuntime(1100): at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
12-12 06:58:53.762: E/AndroidRuntime(1100): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290)
12-12 06:58:53.762: E/AndroidRuntime(1100): at android.app.Activity.setContentView(Activity.java:1928)
12-12 06:58:53.762: E/AndroidRuntime(1100): at com.amaps.androidmap.MainActivity.onCreate(MainActivity.java:12)
12-12 06:58:53.762: E/AndroidRuntime(1100): at android.app.Activity.performCreate(Activity.java:5243)
12-12 06:58:53.762: E/AndroidRuntime(1100): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
12-12 06:58:53.762: E/AndroidRuntime(1100): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
12-12 06:58:53.762: E/AndroidRuntime(1100): ... 11 more
12-12 06:58:53.762: E/AndroidRuntime(1100): Caused by: android.app.Fragment$InstantiationException: Unable to instantiate fragment com.google.android.gms.maps.SupportMapFragment: make sure class name exists, is public, and has an empty constructor that is public
12-12 06:58:53.762: E/AndroidRuntime(1100): at android.app.Fragment.instantiate(Fragment.java:597)
12-12 06:58:53.762: E/AndroidRuntime(1100): at android.app.Fragment.instantiate(Fragment.java:561)
12-12 06:58:53.762: E/AndroidRuntime(1100): at android.app.Activity.onCreateView(Activity.java:4777)
12-12 06:58:53.762: E/AndroidRuntime(1100): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:689)
12-12 06:58:53.762: E/AndroidRuntime(1100): ... 21 more
12-12 06:58:53.762: E/AndroidRuntime(1100): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.google.android.gms.maps.SupportMapFragment" on path: DexPathList[[zip file "/data/app/com.amaps.androidmap-2.apk"],nativeLibraryDirectories=[/data/app-lib/com.amaps.androidmap-2, /system/lib]]
12-12 06:58:53.762: E/AndroidRuntime(1100): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
12-12 06:58:53.762: E/AndroidRuntime(1100): at java.lang.ClassLoader.loadClass(ClassLoader.java:497)
12-12 06:58:53.762: E/AndroidRuntime(1100): at java.lang.ClassLoader.loadClass(ClassLoader.java:457)
12-12 06:58:53.762: E/AndroidRuntime(1100): at android.app.Fragment.instantiate(Fragment.java:583)
12-12 06:58:53.762: E/AndroidRuntime(1100): ... 24 more
12-12 06:59:41.712: I/Process(1100): Sending signal. PID: 1100 SIG: 9
我从路径导入库: ADT-捆绑Linux的-x86-20131030 / SDK /演员/谷歌/ google_play_services / libproject //的Google Play-services_lib / 我也做了参考,但仍然没有发生任何事情。不知道该做什么。
答案 0 :(得分:1)
我怀疑您没有正确引用Google Play服务库项目。确保你先做到这一点。
<uses-sdk
android:minSdkVersion="8"
你的min sdk是8.使用SupportMapFragment
并扩展FragmentActivity
并使用支持库。
或将api级别更改为12及以上。
你也错过了
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
在清单
的应用程序标签中确保您已正确引用Google Play服务库项目,并按照上述链接中的所有步骤进行操作
答案 1 :(得分:0)
替换它:
<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/btnSurrey" />
用这个:
<?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.SupportMapFragment"/>
在maifest中添加:
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
答案 2 :(得分:0)
您的MapFragment应该像这样定义。
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.MapFragment" />
答案 3 :(得分:0)
在AndroidManifest.xml中添加此内容
<meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />