-1

我有此代码用于在 android 设备上显示地图,但在“应用程序地图(进程 com.example.maps)已意外停止。”对话框中显示以下错误。即使按照 developer.google.com/maps 上给出的分步程序进行操作,上述给出的错误也会显示。

package com.example.maps;

import android.os.Bundle;
import android.app.Activity;
//import android.view.Menu;
public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

/*  @Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}*/
}

这是布局......

<?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"/>

这是 LogCat 视图.....

04-08 21:53:15.304:         D/AndroidRuntime(11290): Shutting down VM
04-08 21:53:15.304:         W/dalvikvm(11290): threadid=1: thread exiting with uncaught                                                                       exception (group=0x400205a0)
04-08 21:53:15.304:         E/AndroidRuntime(11290): FATAL EXCEPTION: main
04-08 21:53:15.304:         E/AndroidRuntime(11290): java.lang.RuntimeException: Unable to start activity ComponentInfo com.example.maps/com.example.maps.MainActivity}:                                                     android.view.InflateException: Binary XML file line #2: Error inflating class fragment
04-08 21:53:15.304:         E/AndroidRuntime(11290): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1830)
04-08 21:53:15.304:         E/AndroidRuntime(11290): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1851)
04-08 21:53:15.304:         E/AndroidRuntime(11290): at android.app.ActivityThread.access$1500(ActivityThread.java:132)
04-08 21:53:15.304:         E/AndroidRuntime(11290): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1038)
04-08 21:53:15.304:         E/AndroidRuntime(11290): at android.os.Handler.dispatchMessage(Handler.java:99)
04-08 21:53:15.304:         E/AndroidRuntime(11290): at android.os.Looper.loop(Looper.java:143)
04-08 21:53:15.304:         E/AndroidRuntime(11290): at android.app.ActivityThread.main(ActivityThread.java:4277)
04-08 21:53:15.304:         E/AndroidRuntime(11290): at java.lang.reflect.Method.invokeNative(Native Method)
04-08 21:53:15.304:         E/AndroidRuntime(11290): at java.lang.reflect.Method.invoke(Method.java:507)
04-08 21:53:15.304:         E/AndroidRuntime(11290): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-08 21:53:15.304:         E/AndroidRuntime(11290): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-08 21:53:15.304:         E/AndroidRuntime(11290): at dalvik.system.NativeStart.main(Native Method)
04-08 21:53:15.304:         E/AndroidRuntime(11290): Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class fragment
04-08 21:53:15.304:         E/AndroidRuntime(11290): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:581)
04-08 21:53:15.304:         E/AndroidRuntime(11290): at android.view.LayoutInflater.inflate(LayoutInflater.java:386)
04-08 21:53:15.304:         E/AndroidRuntime(11290): at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
04-08 21:53:15.304:         E/AndroidRuntime(11290): at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
04-08 21:53:15.304:         E/AndroidRuntime(11290): at com.android.internal.policy.impl.PhoneWindow.setContentView     
(PhoneWindow.java:239)
04-08 21:53:15.304:         E/AndroidRuntime(11290): at android.app.Activity.setContentView(Activity.java:1742)
04-08 21:53:15.304:         E/AndroidRuntime(11290): at com.example.maps.MainActivity.onCreate(MainActivity.java:12)
04-08 21:53:15.304:         E/AndroidRuntime(11290): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1072)
04-08 21:53:15.304:         E/AndroidRuntime(11290): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1794)
04-08 21:53:15.304:         E/AndroidRuntime(11290):    ... 11 more
04-08 21:53:15.304:         E/AndroidRuntime(11290): Caused by: java.lang.ClassNotFoundException: android.view.fragment in loader       
dalvik.system.PathClassLoader[/data/app/com.example.maps-1.apk]
04-08 21:53:15.304:         E/AndroidRuntime(11290): at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240)
04-08 21:53:15.304:         E/AndroidRuntime(11290): at java.lang.ClassLoader.loadClass(ClassLoader.java:551)
04-08 21:53:15.304:         E/AndroidRuntime(11290): at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
04-08 21:53:15.304:         E/AndroidRuntime(11290): at android.view.LayoutInflater.createView(LayoutInflater.java:471)
04-08 21:53:15.304:         E/AndroidRuntime(11290): at android.view.LayoutInflater.onCreateView(LayoutInflater.java:549)
04-08 21:53:15.304:         E/AndroidRuntime(11290): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView           
(PhoneLayoutInflater.java:66)
04-08 21:53:15.304:         E/AndroidRuntime(11290): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:568)
04-08 21:53:15.304:         E/AndroidRuntime(11290):    ... 19 more
04-08 21:53:17.406:         I/Process(11290): Sending signal. PID: 11290 SIG: 9
04-08 21:53:34.072:         D/AndroidRuntime(11403): Shutting down VM
04-08 21:53:34.072:         W/dalvikvm(11403): threadid=1: thread exiting with uncaught                                                                exception (group=0x400205a0)
04-08 21:53:34.082:         E/AndroidRuntime(11403): FATAL EXCEPTION: main
04-08 21:53:34.082:         E/AndroidRuntime(11403): java.lang.RuntimeException: Unable                               to start activity ComponentInfo   {com.example.maps/com.example.maps.MainActivity}: android.view.InflateException: Binary XML file line #2: Error inflating class fragment
04-08 21:53:34.082:         E/AndroidRuntime(11403): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1830)
04-08 21:53:34.082:         E/AndroidRuntime(11403): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1851)
04-08 21:53:34.082:         E/AndroidRuntime(11403): at android.app.ActivityThread.access$1500(ActivityThread.java:132)
04-08 21:53:34.082:         E/AndroidRuntime(11403): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1038)
04-08 21:53:34.082:         E/AndroidRuntime(11403): at android.os.Handler.dispatchMessage(Handler.java:99)
04-08 21:53:34.082:         E/AndroidRuntime(11403): at android.os.Looper.loop(Looper.java:143)
04-08 21:53:34.082:         E/AndroidRuntime(11403): at android.app.ActivityThread.main(ActivityThread.java:4277)
04-08 21:53:34.082:         E/AndroidRuntime(11403): at java.lang.reflect.Method.invokeNative(Native Method)
04-08 21:53:34.082:         E/AndroidRuntime(11403): at java.lang.reflect.Method.invoke(Method.java:507)
04-08 21:53:34.082:         E/AndroidRuntime(11403): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-08 21:53:34.082:         E/AndroidRuntime(11403): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-08 21:53:34.082:         E/AndroidRuntime(11403): at dalvik.system.NativeStart.main(Native Method)
04-08 21:53:34.082:         E/AndroidRuntime(11403): Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class fragment
04-08 21:53:34.082:         E/AndroidRuntime(11403): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:581)
04-08 21:53:34.082:         E/AndroidRuntime(11403): at android.view.LayoutInflater.inflate(LayoutInflater.java:386)
04-08 21:53:34.082:         E/AndroidRuntime(11403): at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
04-08 21:53:34.082:         E/AndroidRuntime(11403): at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
04-08 21:53:34.082:         E/AndroidRuntime(11403): at com.android.internal.policy.impl.PhoneWindow.setContentView         
(PhoneWindow.java:239)
04-08 21:53:34.082:         E/AndroidRuntime(11403): at android.app.Activity.setContentView(Activity.java:1742)
04-08 21:53:34.082:         E/AndroidRuntime(11403): at com.example.maps.MainActivity.onCreate(MainActivity.java:12)
04-08 21:53:34.082:         E/AndroidRuntime(11403): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1072)
04-08 21:53:34.082:         E/AndroidRuntime(11403): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1794)
04-08 21:53:34.082:         E/AndroidRuntime(11403):    ... 11 more
04-08 21:53:34.082:         E/AndroidRuntime(11403): Caused by: java.lang.ClassNotFoundException: android.view.fragment in loader       
dalvik.system.PathClassLoader[/data/app/com.example.maps-2.apk]
04-08 21:53:34.082:         E/AndroidRuntime(11403): at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240)
04-08 21:53:34.082:         E/AndroidRuntime(11403): at java.lang.ClassLoader.loadClass(ClassLoader.java:551)
04-08 21:53:34.082:         E/AndroidRuntime(11403): at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
04-08 21:53:34.082:         E/AndroidRuntime(11403): at android.view.LayoutInflater.createView(LayoutInflater.java:471)
04-08 21:53:34.082:         E/AndroidRuntime(11403): at android.view.LayoutInflater.onCreateView(LayoutInflater.java:549)
04-08 21:53:34.082:         E/AndroidRuntime(11403): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView           
(PhoneLayoutInflater.java:66)
04-08 21:53:34.082:         E/AndroidRuntime(11403): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:568)
04-08 21:53:34.082:         E/AndroidRuntime(11403):    ... 19 more
4

1 回答 1

2

您正尝试MapFragment在 Android 2.3 或更低版本的设备上运行的项目中使用。要在 Android 2.3 及更低版本上使用 Maps V2,您需要使用 Android Support 包中的 Fragments backport 并使用SupportMapFragment.

于 2013-04-08T17:12:34.847 回答