2

我有一个活动,我想要做的是开始一个新的活动,它按照这个例子(https://developers.google.com/maps/documentation/android/start)保存一个 Google Maps Fragment API v2。我收到运行时错误

"Binary XML file line #4: Error inflating class fragment" 

这是活动的 XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >    
   <fragment xmlns:android="http://schemas.android.com/apk/res/android"
         android:id="@+id/map"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
         class="com.google.android.gms.maps.MapFragment"/>   
</LinearLayout>

这是活动的课程:

导入android.app.Activity;导入android.os.Bundle;

public class MapsActivity extends Activity {

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.map_act);

    // TODO Auto-generated method stub
}

}

这是logcat:

12-18 11:59:37.584: W/dalvikvm(9480): threadid=1: thread exiting with uncaught    exception (group=0x2b6a3300)
12-18 11:59:37.594: E/AndroidRuntime(9480): FATAL EXCEPTION: main
12-18 11:59:37.594: E/AndroidRuntime(9480): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.dimacroitori.supermarket/com.dimacroitori.supermarket.MapsActivity}: android.view.InflateException: Binary XML file line #4: Error inflating class fragment
12-18 11:59:37.594: E/AndroidRuntime(9480):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2190)
12-18 11:59:37.594: E/AndroidRuntime(9480):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2215)
12-18 11:59:37.594: E/AndroidRuntime(9480):     at android.app.ActivityThread.access$600(ActivityThread.java:144)
12-18 11:59:37.594: E/AndroidRuntime(9480):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
12-18 11:59:37.594: E/AndroidRuntime(9480):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-18 11:59:37.594: E/AndroidRuntime(9480):     at android.os.Looper.loop(Looper.java:137)
12-18 11:59:37.594: E/AndroidRuntime(9480):     at android.app.ActivityThread.main(ActivityThread.java:4939)
12-18 11:59:37.594: E/AndroidRuntime(9480):     at java.lang.reflect.Method.invokeNative(Native Method)
12-18 11:59:37.594: E/AndroidRuntime(9480):     at java.lang.reflect.Method.invoke(Method.java:511)
12-18 11:59:37.594: E/AndroidRuntime(9480):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
12-18 11:59:37.594: E/AndroidRuntime(9480):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558)
12-18 11:59:37.594: E/AndroidRuntime(9480):     at dalvik.system.NativeStart.main(Native Method)
12-18 11:59:37.594: E/AndroidRuntime(9480): Caused by: android.view.InflateException: Binary XML file line #4: Error inflating class fragment
12-18 11:59:37.594: E/AndroidRuntime(9480):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
12-18 11:59:37.594: E/AndroidRuntime(9480):     at android.view.LayoutInflater.inflate(LayoutInflater.java:466)
12-18 11:59:37.594: E/AndroidRuntime(9480):     at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
12-18 11:59:37.594: E/AndroidRuntime(9480):     at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
12-18 11:59:37.594: E/AndroidRuntime(9480):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:257)
12-18 11:59:37.594: E/AndroidRuntime(9480):     at android.app.Activity.setContentView(Activity.java:1867)
12-18 11:59:37.594: E/AndroidRuntime(9480):     at com.exmaple.MapsActivity.onCreate(MapsActivity.java:12)
12-18 11:59:37.594: E/AndroidRuntime(9480):     at android.app.Activity.performCreate(Activity.java:5008)
12-18 11:59:37.594: E/AndroidRuntime(9480):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
12-18 11:59:37.594: E/AndroidRuntime(9480):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
12-18 11:59:37.594: E/AndroidRuntime(9480):     ... 11 more
12-18 11:59:37.594: E/AndroidRuntime(9480): 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-18 11:59:37.594: E/AndroidRuntime(9480):     at android.app.Fragment.instantiate(Fragment.java:584)
12-18 11:59:37.594: E/AndroidRuntime(9480):     at android.app.Fragment.instantiate(Fragment.java:552)
12-18 11:59:37.594: E/AndroidRuntime(9480):     at android.app.Activity.onCreateView(Activity.java:4656)
12-18 11:59:37.594: E/AndroidRuntime(9480):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
12-18 11:59:37.594: E/AndroidRuntime(9480):     ... 20 more
12-18 11:59:37.594: E/AndroidRuntime(9480): Caused by: java.lang.ClassNotFoundException: com.google.android.gms.maps.MapFragment
12-18 11:59:37.594: E/AndroidRuntime(9480):     at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
12-18 11:59:37.594: E/AndroidRuntime(9480):     at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
12-18 11:59:37.594: E/AndroidRuntime(9480):     at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
12-18 11:59:37.594: E/AndroidRuntime(9480):     at android.app.Fragment.instantiate(Fragment.java:574)
12-18 11:59:37.594: E/AndroidRuntime(9480):     ... 23 more

有人可以解释我做错了什么吗?

4

1 回答 1

0

确保您已将Google Play Services Android 库项目添加到定义该类的应用程序中。还要确保您在 API 级别 11 或更高级别的环境中运行您的应用程序,因为您正在尝试使用本机片段(而不是 Android 支持包的向后移植)。

于 2012-12-18T12:22:21.703 回答