实施Google Maps Android API V2时出错 - 无法解析SupportMapFragment

时间:2013-06-26 11:47:13

标签: android google-maps-android-api-2

Android 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密钥 - 打开Goog​​le地图Android API V2。
  2. 将google-play-services_lib导入项目,并将其添加为库。
  3. 由于我的目标是3.0以下的版本,我已经阅读了一些我应该使用ActionbarSherlock的地方,所以我也将其导入并添加为库。
  4. 请查看下面的截图和代码,让我知道,我哪里出错了。 我已经读过,我需要在XML中使用“SupportMapFragment”并在类文件中“扩展FragmentActivity”,但我没有让它正常工作。

    enter image description here

    enter image description here

    的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>
    

    maps.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();
    

    如果您需要任何信息,请与我们联系。

    问题截图:

    enter image description here

    感谢您的帮助!!!

    根据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
    

    订单和导出标签

    enter image description here

3 个答案:

答案 0 :(得分:9)

也许您错过了import的{​​{1}}声明。

您可能还希望确认,如果您使用的是R22工具your "Order & Export" portion of the Eclipse build path dialog is properly configured

答案 1 :(得分:-1)

我不这么认为以下代码可以正常工作

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

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

map = (SupportMapFragment)  getChildFragmentManager().findFragmentById(R.id.map);

答案 2 :(得分:-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 - &gt;使用清单: <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="15" />