我正在为Android创建示例地图,但我有问题这是我的问题
10-13 06:31:26.923: D/AndroidRuntime(8754): Shutting down VM
10-13 06:31:26.923: W/dalvikvm(8754): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
10-13 06:31:26.963: E/AndroidRuntime(8754): FATAL EXCEPTION: main
10-13 06:31:26.963: E/AndroidRuntime(8754): java.lang.RuntimeException: Unable to start activity ComponentInfo{fuad.viewmaps/fuad.viewmaps.MainActivity}: android.view.InflateException: Binary XML file line #13: Error inflating class fragment
10-13 06:31:26.963: E/AndroidRuntime(8754): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
10-13 06:31:26.963: E/AndroidRuntime(8754): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
10-13 06:31:26.963: E/AndroidRuntime(8754): at android.app.ActivityThread.access$600(ActivityThread.java:141)
10-13 06:31:26.963: E/AndroidRuntime(8754): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
10-13 06:31:26.963: E/AndroidRuntime(8754): at android.os.Handler.dispatchMessage(Handler.java:99)
10-13 06:31:26.963: E/AndroidRuntime(8754): at android.os.Looper.loop(Looper.java:137)
10-13 06:31:26.963: E/AndroidRuntime(8754): at android.app.ActivityThread.main(ActivityThread.java:5041)
10-13 06:31:26.963: E/AndroidRuntime(8754): at java.lang.reflect.Method.invokeNative(Native Method)
10-13 06:31:26.963: E/AndroidRuntime(8754): at java.lang.reflect.Method.invoke(Method.java:511)
10-13 06:31:26.963: E/AndroidRuntime(8754): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
10-13 06:31:26.963: E/AndroidRuntime(8754): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
10-13 06:31:26.963: E/AndroidRuntime(8754): at dalvik.system.NativeStart.main(Native Method)
10-13 06:31:26.963: E/AndroidRuntime(8754): Caused by: android.view.InflateException: Binary XML file line #13: Error inflating class fragment
10-13 06:31:26.963: E/AndroidRuntime(8754): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
10-13 06:31:26.963: E/AndroidRuntime(8754): at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
10-13 06:31:26.963: E/AndroidRuntime(8754): at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
10-13 06:31:26.963: E/AndroidRuntime(8754): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
10-13 06:31:26.963: E/AndroidRuntime(8754): at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
10-13 06:31:26.963: E/AndroidRuntime(8754): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:270)
10-13 06:31:26.963: E/AndroidRuntime(8754): at android.app.Activity.setContentView(Activity.java:1881)
10-13 06:31:26.963: E/AndroidRuntime(8754): at fuad.viewmaps.MainActivity.onCreate(MainActivity.java:12)
10-13 06:31:26.963: E/AndroidRuntime(8754): at android.app.Activity.performCreate(Activity.java:5104)
10-13 06:31:26.963: E/AndroidRuntime(8754): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
10-13 06:31:26.963: E/AndroidRuntime(8754): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
10-13 06:31:26.963: E/AndroidRuntime(8754): ... 11 more
10-13 06:31:26.963: E/AndroidRuntime(8754): Caused by: android.app.Fragment$InstantiationException: Unable to instantiate fragment com.google.android.gms.map.MapFragment: make sure class name exists, is public, and has an empty constructor that is public
10-13 06:31:26.963: E/AndroidRuntime(8754): at android.app.Fragment.instantiate(Fragment.java:592)
10-13 06:31:26.963: E/AndroidRuntime(8754): at android.app.Fragment.instantiate(Fragment.java:560)
10-13 06:31:26.963: E/AndroidRuntime(8754): at android.app.Activity.onCreateView(Activity.java:4709)
10-13 06:31:26.963: E/AndroidRuntime(8754): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
10-13 06:31:26.963: E/AndroidRuntime(8754): ... 21 more
10-13 06:31:26.963: E/AndroidRuntime(8754): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.google.android.gms.map.MapFragment" on path: /data/app/fuad.viewmaps-2.apk
10-13 06:31:26.963: E/AndroidRuntime(8754): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:65)
10-13 06:31:26.963: E/AndroidRuntime(8754): at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
10-13 06:31:26.963: E/AndroidRuntime(8754): at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
10-13 06:31:26.963: E/AndroidRuntime(8754): at android.app.Fragment.instantiate(Fragment.java:582)
10-13 06:31:26.963: E/AndroidRuntime(8754): ... 24 more
10-13 06:31:29.103: I/Process(8754): Sending signal. PID: 8754 SIG: 9
这是我的问题
我的代码
MainActivity.java
package fuad.viewmaps;
import android.os.Bundle;
import android.app.Activity;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView
(R.layout.activity_main); }
}
activity_main.xml中
<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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="fuad.viewmaps.MainActivity" >
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.map.MapFragment"/>
</RelativeLayout>
的AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="fuad.viewmaps"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<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.maps.v2.API_KEY"
android:value="AIzaSyCuhxy29DShixyeHjzrB8NxfrQv9TCCZZI"/>
<activity
android:name=".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>
<uses-feature
android:glEsVersion="0x00020000"
android:required="true"/>
<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"/>
<!-- the following two permission are not required to use Google maps Android Api v2,
but are recomended -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
</manifest>
答案 0 :(得分:2)
因为你的android:minSdkVersion="8"
。所以,你应该改变这个
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.map.MapFragment"/>
到
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.map.SupportMapFragment"/>
并且还要改变
public class MainActivity extends Activity
到
public class MainActivity extends FragmentActivity
另外,您应该在<meta-data>
的{{1}}标记下添加Google Play服务版本<application>
标记。
manifest.xml