长期以来,我一直在尝试在简单的应用程序中使用 Google Map API V2。我尝试了很多次,但不断收到错误,例如
“无法实例化片段 com.google.android.gms.maps.MapFragment:确保类名存在,是公共的,并且有一个公共的空构造函数”
以下代码,我认为它来自链接。
activity_google_map.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"/>'
谷歌地图.java
package com.agilissystems.googlemap;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class GoogleMap extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_google_map);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.google_map, menu);
return true;
}
}
AndroidManifest.xml
`<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.agilissystems.googlemap"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="17" />
<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="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
<uses-feature android:glEsVersion="2" 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.agilissystems.googlemap.GoogleMap"
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="removed"/>
</application>
</manifest>
LogCat 显示以下错误:
07-19 20:53:57.928: D/AndroidRuntime(13864): Shutting down VM
07-19 20:53:57.928: W/dalvikvm(13864): threadid=1: thread exiting with uncaught exception (group=0x41c6e2a0)
07-19 20:53:57.936: E/AndroidRuntime(13864): FATAL EXCEPTION: main
07-19 20:53:57.936: E/AndroidRuntime(13864): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.agilissystems.googlemap/com.agilissystems.googlemap.GoogleMap}: android.view.InflateException: Binary XML file line #2: Error inflating class fragment
07-19 20:53:57.936: E/AndroidRuntime(13864): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
07-19 20:53:57.936: E/AndroidRuntime(13864): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2122)
07-19 20:53:57.936: E/AndroidRuntime(13864): at android.app.ActivityThread.access$600(ActivityThread.java:140)
07-19 20:53:57.936: E/AndroidRuntime(13864): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1228)
07-19 20:53:57.936: E/AndroidRuntime(13864): at android.os.Handler.dispatchMessage(Handler.java:99)
07-19 20:53:57.936: E/AndroidRuntime(13864): at android.os.Looper.loop(Looper.java:137)
07-19 20:53:57.936: E/AndroidRuntime(13864): at android.app.ActivityThread.main(ActivityThread.java:4895)
07-19 20:53:57.936: E/AndroidRuntime(13864): at java.lang.reflect.Method.invokeNative(Native Method)
07-19 20:53:57.936: E/AndroidRuntime(13864): at java.lang.reflect.Method.invoke(Method.java:511)
07-19 20:53:57.936: E/AndroidRuntime(13864): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:994)
07-19 20:53:57.936: E/AndroidRuntime(13864): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761)
07-19 20:53:57.936: E/AndroidRuntime(13864): at dalvik.system.NativeStart.main(Native Method)
07-19 20:53:57.936: E/AndroidRuntime(13864): Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class fragment
07-19 20:53:57.936: E/AndroidRuntime(13864): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
07-19 20:53:57.936: E/AndroidRuntime(13864): at android.view.LayoutInflater.inflate(LayoutInflater.java:466)
07-19 20:53:57.936: E/AndroidRuntime(13864): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
07-19 20:53:57.936: E/AndroidRuntime(13864): at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
07-19 20:53:57.936: E/AndroidRuntime(13864): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:306)
07-19 20:53:57.936: E/AndroidRuntime(13864): at android.app.Activity.setContentView(Activity.java:1912)
07-19 20:53:57.936: E/AndroidRuntime(13864): at com.agilissystems.googlemap.GoogleMap.onCreate(GoogleMap.java:16)
07-19 20:53:57.936: E/AndroidRuntime(13864): at android.app.Activity.performCreate(Activity.java:5163)
07-19 20:53:57.936: E/AndroidRuntime(13864): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
07-19 20:53:57.936: E/AndroidRuntime(13864): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2061)
07-19 20:53:57.936: E/AndroidRuntime(13864): ... 11 more
07-19 20:53:57.936: E/AndroidRuntime(13864): 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
07-19 20:53:57.936: E/AndroidRuntime(13864): at android.app.Fragment.instantiate(Fragment.java:584)
07-19 20:53:57.936: E/AndroidRuntime(13864): at android.app.Fragment.instantiate(Fragment.java:552)
07-19 20:53:57.936: E/AndroidRuntime(13864): at android.app.Activity.onCreateView(Activity.java:4806)
07-19 20:53:57.936: E/AndroidRuntime(13864): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
07-19 20:53:57.936: E/AndroidRuntime(13864): ... 20 more
07-19 20:53:57.936: E/AndroidRuntime(13864): Caused by: java.lang.ClassNotFoundException: com.google.android.gms.maps.MapFragment
07-19 20:53:57.936: E/AndroidRuntime(13864): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
07-19 20:53:57.936: E/AndroidRuntime(13864): at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
07-19 20:53:57.936: E/AndroidRuntime(13864): at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
07-19 20:53:57.936: E/AndroidRuntime(13864): at android.app.Fragment.instantiate(Fragment.java:574)
07-19 20:53:57.936: E/AndroidRuntime(13864): ... 23 more
07-19 20:54:06.295: I/Process(13864): Sending signal. PID: 13864 SIG: 9