我已经在我的xml布局中添加了一个mapview,其节点如下:包含在框架内
<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>
似乎无法找出为什么它认为它的框架视图
答案 0 :(得分:4)
这是MapFragment
,而不是MapView
。您应该查看MapFragment
的文档,在MapFragment
中执行相同的操作。
所以有可能这样:
MapFragment map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
答案 1 :(得分:4)
在你的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();