2

安卓 2.3.3

我已经阅读了很多关于同一问题的问题,但是我阅读的越多,我就越困惑。所以,我把我的问题放在这里,如果它是重复的,请多多包涵,但我真的想不通。

I have minSdkVersion=8 and targetSdkVersion=17 and 
I am targeting devices from Android version 2.2 and above

这是我所做的事情的清单:

  1. 注册 Google API 控制台 - 获得 API 密钥 - 开启 Google Maps Android API V2。
  2. 将 google-play-services_lib 导入项目,将其添加为库。
  3. 由于我的目标是 3.0 以下的版本,我已经阅读了一些我应该使用 ActionbarSherlock 的地方,所以我也将其导入并添加为库。

请查看下面的屏幕截图和代码,让我知道我哪里出错了。我已经读过,我需要在 XML 中使用“SupportMapFragment”并在类文件中使用“extend FragmentActivity”,但我没有让它正常工作。

在此处输入图像描述

在此处输入图像描述

AndroidManifest.XML

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.xx.xxx"
        android:versionCode="1"
        android:versionName="1.0" >

        <uses-sdk
            android:minSdkVersion="8"
            android:targetSdkVersion="17" />

        <permission
            android:name="com.xx.xxx.permission.MAPS_RECEIVE"
            android:protectionLevel="signature" />

        <uses-feature
            android:glEsVersion="0x00020000"
            android:required="true" />

        <uses-permission android:name="com.xx.xxx.permission.MAPS_RECEIVE" />
        <uses-permission android:name="android.permission.INTERNET" />
        <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
        <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
        <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
        <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />


        <application
            android:name=".xxxApp"
            android:allowBackup="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/appName" >
            <uses-library android:name="com.google.android.maps" />

            <activity
                android:name=".SplashActivity"
                android:screenOrientation="portrait"
                android:theme="@android:style/Theme.Light.NoTitleBar.Fullscreen" >
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />

                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>

//All Activities are declared here, including the MapsActivity

            <!-- Google Maps APIV2  Key -->
            <meta-data
                android:name="com.google.android.maps.v2.API_KEY"
                android:value="My_API_KEY(Removed)" />

        </application>

    </manifest>

地图.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" >

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:contentDescription="@string/imageDescription"
                android:scaleType="fitXY"
                android:src="@drawable/nav_bar_background" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:text="@string/map"
                android:textColor="#FFFFFF"
                android:textSize="20sp"
                android:textStyle="bold" />
        </RelativeLayout>

        <fragment
            android:id="@+id/map"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
//changed this to support versions < 3.0
            class="com.google.android.gms.maps.SupportMapFragment" />

    </LinearLayout>

MapsActivity.xml

    public class MapsActivity extends FragmentActivity {
        private MapView mapView;
        private GoogleAnalyticsTracker tracker;
        private GeoPoint point;
        private OverlayItem overlayitem;

        static final LatLng hacc_position = new LatLng(25.981343, -80.161756);
        //static final LatLng KIEL = new LatLng(53.551, 9.993);
        private GoogleMap map;

        @Override
        protected void onCreate(Bundle bundle) {
            super.onCreate(bundle);
            setContentView(R.layout.maps);

  **//Error at below line :  SupportMapFragment cannot be resolved**

            map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
                    .getMap();

            Marker hacc = map.addMarker(new MarkerOptions().position(hacc_position)
                    .title("XYZ"));

            // Move the camera instantly to hacc with a zoom of 15.
            map.moveCamera(CameraUpdateFactory.newLatLngZoom(hacc_position, 15));

            // Zoom in, animating the camera.
            map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
            }

我不确定如何解决这个问题。我必须支持 2.2 及更高版本的设备,这是强制性的。

有人可以帮忙吗。

我无法执行该项目,因为我有错误

map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
                        .getMap();

如果您需要任何信息,请告诉我。

问题截图:

在此处输入图像描述

谢谢您的帮助!!!

根据 CommonsWare 的建议,我已经导入了包并且错误消失了。当我运行应用程序时,我得到一个 ClassNotFoundException。请查看 logcat 跟踪。

06-26 17:27:15.759: E/Crittercism(19853): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.xx.xxx/com.xx.xxx.MapsActivity}: android.view.InflateException: Binary XML file line #28: Error inflating class fragment
06-26 17:27:15.759: E/Crittercism(19853):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
06-26 17:27:15.759: E/Crittercism(19853):   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
06-26 17:27:15.759: E/Crittercism(19853):   at android.app.ActivityThread.access$1500(ActivityThread.java:117)
06-26 17:27:15.759: E/Crittercism(19853):   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
06-26 17:27:15.759: E/Crittercism(19853):   at android.os.Handler.dispatchMessage(Handler.java:99)
06-26 17:27:15.759: E/Crittercism(19853):   at android.os.Looper.loop(Looper.java:130)
06-26 17:27:15.759: E/Crittercism(19853):   at android.app.ActivityThread.main(ActivityThread.java:3687)
06-26 17:27:15.759: E/Crittercism(19853):   at java.lang.reflect.Method.invokeNative(Native Method)
06-26 17:27:15.759: E/Crittercism(19853):   at java.lang.reflect.Method.invoke(Method.java:507)
06-26 17:27:15.759: E/Crittercism(19853):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:878)
06-26 17:27:15.759: E/Crittercism(19853):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:636)
06-26 17:27:15.759: E/Crittercism(19853):   at dalvik.system.NativeStart.main(Native Method)
06-26 17:27:15.759: E/Crittercism(19853): Caused by: android.view.InflateException: Binary XML file line #28: Error inflating class fragment
06-26 17:27:15.759: E/Crittercism(19853):   at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:581)
06-26 17:27:15.759: E/Crittercism(19853):   at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
06-26 17:27:15.759: E/Crittercism(19853):   at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
06-26 17:27:15.759: E/Crittercism(19853):   at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
06-26 17:27:15.759: E/Crittercism(19853):   at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
06-26 17:27:15.759: E/Crittercism(19853):   at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:207)
06-26 17:27:15.759: E/Crittercism(19853):   at android.app.Activity.setContentView(Activity.java:1657)
06-26 17:27:15.759: E/Crittercism(19853):   at com.waspmobile.haccoclc.MapsActivity.onCreate(MapsActivity.java:54)
06-26 17:27:15.759: E/Crittercism(19853):   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
06-26 17:27:15.759: E/Crittercism(19853):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
06-26 17:27:15.759: E/Crittercism(19853):   ... 11 more
06-26 17:27:15.759: E/Crittercism(19853): Caused by: java.lang.ClassNotFoundException: android.view.fragment in loader dalvik.system.PathClassLoader[/system/framework/com.google.android.maps.jar:/data/app/com.xx.xxx-1.apk]
06-26 17:27:15.759: E/Crittercism(19853):   at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240)
06-26 17:27:15.759: E/Crittercism(19853):   at java.lang.ClassLoader.loadClass(ClassLoader.java:551)
06-26 17:27:15.759: E/Crittercism(19853):   at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
06-26 17:27:15.759: E/Crittercism(19853):   at android.view.LayoutInflater.createView(LayoutInflater.java:471)
06-26 17:27:15.759: E/Crittercism(19853):   at android.view.LayoutInflater.onCreateView(LayoutInflater.java:549)
06-26 17:27:15.759: E/Crittercism(19853):   at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:66)
06-26 17:27:15.759: E/Crittercism(19853):   at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:568)
06-26 17:27:15.759: E/Crittercism(19853):   ... 20 more
06-26 17:27:15.959: E/AndroidRuntime(19853): FATAL EXCEPTION: main
06-26 17:27:15.959: E/AndroidRuntime(19853): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.xx.xxx/com.xx.xxx.MapsActivity}: android.view.InflateException: Binary XML file line #28: Error inflating class fragment
06-26 17:27:15.959: E/AndroidRuntime(19853):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
06-26 17:27:15.959: E/AndroidRuntime(19853):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
06-26 17:27:15.959: E/AndroidRuntime(19853):    at android.app.ActivityThread.access$1500(ActivityThread.java:117)
06-26 17:27:15.959: E/AndroidRuntime(19853):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
06-26 17:27:15.959: E/AndroidRuntime(19853):    at android.os.Handler.dispatchMessage(Handler.java:99)
06-26 17:27:15.959: E/AndroidRuntime(19853):    at android.os.Looper.loop(Looper.java:130)
06-26 17:27:15.959: E/AndroidRuntime(19853):    at android.app.ActivityThread.main(ActivityThread.java:3687)
06-26 17:27:15.959: E/AndroidRuntime(19853):    at java.lang.reflect.Method.invokeNative(Native Method)
06-26 17:27:15.959: E/AndroidRuntime(19853):    at java.lang.reflect.Method.invoke(Method.java:507)
06-26 17:27:15.959: E/AndroidRuntime(19853):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:878)
06-26 17:27:15.959: E/AndroidRuntime(19853):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:636)
06-26 17:27:15.959: E/AndroidRuntime(19853):    at dalvik.system.NativeStart.main(Native Method)
06-26 17:27:15.959: E/AndroidRuntime(19853): Caused by: android.view.InflateException: Binary XML file line #28: Error inflating class fragment
06-26 17:27:15.959: E/AndroidRuntime(19853):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:581)
06-26 17:27:15.959: E/AndroidRuntime(19853):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
06-26 17:27:15.959: E/AndroidRuntime(19853):    at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
06-26 17:27:15.959: E/AndroidRuntime(19853):    at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
06-26 17:27:15.959: E/AndroidRuntime(19853):    at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
06-26 17:27:15.959: E/AndroidRuntime(19853):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:207)
06-26 17:27:15.959: E/AndroidRuntime(19853):    at android.app.Activity.setContentView(Activity.java:1657)
06-26 17:27:15.959: E/AndroidRuntime(19853):    at com.xx.xxx.MapsActivity.onCreate(MapsActivity.java:54)
06-26 17:27:15.959: E/AndroidRuntime(19853):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
06-26 17:27:15.959: E/AndroidRuntime(19853):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
06-26 17:27:15.959: E/AndroidRuntime(19853):    ... 11 more
06-26 17:27:15.959: E/AndroidRuntime(19853): Caused by: java.lang.ClassNotFoundException: android.view.fragment in loader dalvik.system.PathClassLoader[/system/framework/com.google.android.maps.jar:/data/app/com.xx.xxx-1.apk]
06-26 17:27:15.959: E/AndroidRuntime(19853):    at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240)
06-26 17:27:15.959: E/AndroidRuntime(19853):    at java.lang.ClassLoader.loadClass(ClassLoader.java:551)
06-26 17:27:15.959: E/AndroidRuntime(19853):    at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
06-26 17:27:15.959: E/AndroidRuntime(19853):    at android.view.LayoutInflater.createView(LayoutInflater.java:471)
06-26 17:27:15.959: E/AndroidRuntime(19853):    at android.view.LayoutInflater.onCreateView(LayoutInflater.java:549)
06-26 17:27:15.959: E/AndroidRuntime(19853):    at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:66)
06-26 17:27:15.959: E/AndroidRuntime(19853):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:568)
06-26 17:27:15.959: E/AndroidRuntime(19853):    ... 20 more

订单和导出选项卡

在此处输入图像描述

4

3 回答 3

9

也许你错过了importfor 的声明com.google.android.gms.maps.SupportMapFragment

如果您使用的是 R22 工具,您可能还希望确认Eclipse 构建路径对话框的“Order & Export”部分已正确配置

于 2013-06-26T11:55:26.063 回答
-1

我不认为下面的代码会起作用

map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
                .getMap();

您应该使用getChildFragmentManager();来检索片段。

map = (SupportMapFragment)  getChildFragmentManager().findFragmentById(R.id.map);
于 2013-06-26T12:30:13.937 回答
-1

@Vamsi Challa 我通过引入 import com.google.android.gms.maps.MapFragment 在我的 ECLIPSE IDE 中解决了您的问题;然后改变

map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();

map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();

然后它为我解决了这个问题。PS --> 清单正在使用: <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="15" />

于 2014-08-07T19:44:52.517 回答