2

我在我的 xml 布局中添加了一个地图视图,其中包含如下节点:包含在框架内

<fragment 
          android:id="@+id/map"
          android:layout_width="100dip"
          android:layout_height="100dip"
          android:name="com.google.android.gms.maps.MapFragment"/>

在我的活动中,我在 onCreate 中引用了它,如下所示:

 map = (MapView) findViewById(R.id.map);

地图在全局中定义为 MapView 地图;

06-02 14:51:12.357: E/AndroidRuntime(32368): FATAL EXCEPTION: main
06-02 14:51:12.357: E/AndroidRuntime(32368): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.KingNozzle/com.KingNozzle.KNAugmentedRealityActivity}: java.lang.ClassCastException: android.widget.FrameLayout cannot be cast to com.google.android.gms.maps.MapView
06-02 14:51:12.357: E/AndroidRuntime(32368):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
06-02 14:51:12.357: E/AndroidRuntime(32368):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
06-02 14:51:12.357: E/AndroidRuntime(32368):    at android.app.ActivityThread.access$600(ActivityThread.java:130)
06-02 14:51:12.357: E/AndroidRuntime(32368):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
06-02 14:51:12.357: E/AndroidRuntime(32368):    at android.os.Handler.dispatchMessage(Handler.java:99)
06-02 14:51:12.357: E/AndroidRuntime(32368):    at android.os.Looper.loop(Looper.java:137)
06-02 14:51:12.357: E/AndroidRuntime(32368):    at android.app.ActivityThread.main(ActivityThread.java:4745)
06-02 14:51:12.357: E/AndroidRuntime(32368):    at java.lang.reflect.Method.invokeNative(Native Method)
06-02 14:51:12.357: E/AndroidRuntime(32368):    at java.lang.reflect.Method.invoke(Method.java:511)
06-02 14:51:12.357: E/AndroidRuntime(32368):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
06-02 14:51:12.357: E/AndroidRuntime(32368):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
06-02 14:51:12.357: E/AndroidRuntime(32368):    at dalvik.system.NativeStart.main(Native Method)
06-02 14:51:12.357: E/AndroidRuntime(32368): Caused by: java.lang.ClassCastException: android.widget.FrameLayout cannot be cast to com.google.android.gms.maps.MapView
06-02 14:51:12.357: E/AndroidRuntime(32368):    at com.KingNozzle.KNAugmentedRealityActivity.onCreate(KNAugmentedRealityActivity.java:129)
06-02 14:51:12.357: E/AndroidRuntime(32368):    at android.app.Activity.performCreate(Activity.java:5008)
06-02 14:51:12.357: E/AndroidRuntime(32368):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
06-02 14:51:12.357: E/AndroidRuntime(32368):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
06-02 14:51:12.357: E/AndroidRuntime(32368):    ... 11 more

我的整个布局看起来像这样:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_vertical" >

    <FrameLayout
        android:id="@+id/arFrame"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" >
    </FrameLayout>

    <TextView
        android:id="@+id/textViewLocation"
        android:layout_width="fill_parent"
        android:layout_height="20dp"
        android:text="no signal"
        android:textAppearance="?android:attr/textAppearanceSmall" />

    <fragment 

          android:id="@+id/map"
          android:layout_width="100dip"
          android:layout_height="100dip"
          android:name="com.google.android.gms.maps.MapFragment">
          </fragment>

    <com.KingNozzle.Views.KNMultiDirectionalSlidingDrawer
        xmlns:my="http://schemas.android.com/apk/res/com.KingNozzle"
        android:id="@+id/drawer"
        my:direction="topToBottom"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        my:handle="@+id/handle"
        my:content="@+id/content">

       <include
           android:id="@id/content"
           android:layout_width="fill_parent"
           android:layout_height="fill_parent"
           layout="@layout/empty_rel" />



        <ImageView
            android:id="@id/handle"
            android:layout_width="wrap_content"
            android:layout_height="40px"
            android:src="@drawable/sliding_drawer_handle_bottom" />
    </com.KingNozzle.Views.KNMultiDirectionalSlidingDrawer>

</RelativeLayout>

似乎无法找出为什么它认为它是框架视图

4

2 回答 2

5

在你的 xml 中将 MapFragment 更改为 SupportMapFragment

<fragment 
      android:id="@+id/map"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      class="com.google.android.gms.maps.SupportMapFragment"/>

在您的代码中

GoogleMap mMap = ((SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map)).getMap();
于 2013-06-03T08:41:43.823 回答
4

这是一个MapFragment,不是一个MapView。您应该查看在 中MapFragment执行相同操作的文档MapFragment

所以有可能是这样的:

MapFragment map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
于 2013-06-02T19:15:25.637 回答