我有两天研究这个并尝试了所有解决方案,但没有任何对我有用。 以下是我的代码:
logcat的:
12-27 19:12:13.950: D/AndroidRuntime(30835): Shutting down VM
12-27 19:12:13.950: W/dalvikvm(30835): threadid=1: thread exiting with uncaught exception (group=0x40ef62a0)
12-27 19:12:13.955: E/AndroidRuntime(30835): FATAL EXCEPTION: main
12-27 19:12:13.955: E/AndroidRuntime(30835): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.me.mystatus/com.me.mystatus.MainActivity}: android.view.InflateException: Binary XML file line #2: Error inflating class fragment
12-27 19:12:13.955: E/AndroidRuntime(30835): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2100)
12-27 19:12:13.955: E/AndroidRuntime(30835): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
12-27 19:12:13.955: E/AndroidRuntime(30835): at android.app.ActivityThread.access$600(ActivityThread.java:140)
12-27 19:12:13.955: E/AndroidRuntime(30835): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
12-27 19:12:13.955: E/AndroidRuntime(30835): at android.os.Handler.dispatchMessage(Handler.java:99)
12-27 19:12:13.955: E/AndroidRuntime(30835): at android.os.Looper.loop(Looper.java:137)
12-27 19:12:13.955: E/AndroidRuntime(30835): at android.app.ActivityThread.main(ActivityThread.java:4898)
12-27 19:12:13.955: E/AndroidRuntime(30835): at java.lang.reflect.Method.invokeNative(Native Method)
12-27 19:12:13.955: E/AndroidRuntime(30835): at java.lang.reflect.Method.invoke(Method.java:511)
12-27 19:12:13.955: E/AndroidRuntime(30835): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1008)
12-27 19:12:13.955: E/AndroidRuntime(30835): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:775)
12-27 19:12:13.955: E/AndroidRuntime(30835): at dalvik.system.NativeStart.main(Native Method)
12-27 19:12:13.955: E/AndroidRuntime(30835): Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class fragment
12-27 19:12:13.955: E/AndroidRuntime(30835): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
12-27 19:12:13.955: E/AndroidRuntime(30835): at android.view.LayoutInflater.inflate(LayoutInflater.java:466)
12-27 19:12:13.955: E/AndroidRuntime(30835): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
12-27 19:12:13.955: E/AndroidRuntime(30835): at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
12-27 19:12:13.955: E/AndroidRuntime(30835): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:308)
12-27 19:12:13.955: E/AndroidRuntime(30835): at android.app.Activity.setContentView(Activity.java:1924)
12-27 19:12:13.955: E/AndroidRuntime(30835): at com.me.mystatus.MainActivity.onCreate(MainActivity.java:12)
12-27 19:12:13.955: E/AndroidRuntime(30835): at android.app.Activity.performCreate(Activity.java:5206)
12-27 19:12:13.955: E/AndroidRuntime(30835): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
12-27 19:12:13.955: E/AndroidRuntime(30835): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064)
12-27 19:12:13.955: E/AndroidRuntime(30835): ... 11 more
12-27 19:12:13.955: E/AndroidRuntime(30835): 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-27 19:12:13.955: E/AndroidRuntime(30835): at android.app.Fragment.instantiate(Fragment.java:584)
12-27 19:12:13.955: E/AndroidRuntime(30835): at android.app.Fragment.instantiate(Fragment.java:552)
12-27 19:12:13.955: E/AndroidRuntime(30835): at android.app.Activity.onCreateView(Activity.java:4849)
12-27 19:12:13.955: E/AndroidRuntime(30835): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
12-27 19:12:13.955: E/AndroidRuntime(30835): ... 20 more
以下是我的 的 MainActivity.java
包com.me.mystatus;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
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);
}
}
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.SupportMapFragment"/>
的Manifest.xml :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.me.mystatus"
android:versionCode="1"
android:versionName="1.0" >
<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-feature
android:glEsVersion="0x00020000"
android:required="true"/>
<uses-sdk
android:minSdkVersion="12"
android:targetSdkVersion="16" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.me.mystatus.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.gms.version"
android:value="@integer/google_play_services_version" />
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="myvalue"/>
</application>
</manifest>
请帮助。
答案 0 :(得分:1)
您的SupportMapFragmenr无法识别,请确保将google-play-services库添加为工作区中的项目并添加到项目的库中(右键单击项目 - &gt;属性 - &gt; android - &gt;库)。有时,您必须在项目的lib文件夹中添加google-play-services库.jar(取自play-services库项目的bin文件夹)。
答案 1 :(得分:1)
使用class而不是android:name
机器人:名称= “com.google.android.gms.maps.SupportMapFragment”/&GT;
应改为
class =“com.google.android.gms.maps.SupportMapFragment”/
确保文件路径正确,因为在编译过程中,eclipse不会检查路径。
其他参考资料是link。
答案 2 :(得分:0)
仅供记录。
别忘了包含在AndroidManifest.xml中
<meta-data android:name="com.google.android.geo.API_KEY"
android:value="KEY" />