我遇到了完全相同的问题。所提供的示例为:
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment"
map:cameraBearing="112.5"
map:cameraTargetLat="-33.796923"
map:cameraTargetLng="150.922433"
map:cameraTilt="30"
map:cameraZoom="13"
map:mapType="normal"
map:uiCompass="false"
map:uiRotateGestures="true"
map:uiScrollGestures="false"
map:uiTiltGestures="true"
map:uiZoomControls="false"
map:uiZoomGestures="true"/>
这段代码本身是没有问题的,但是如果想要将它加入到父元素中,父元素会拒绝接受 xmlns 属性。如果你将 xmlns 声明移到顶级元素之后,XML 片段仍然无法接受 map 前缀。
Unexpected namespace prefix "map" found for tag fragment
现在,如果您扩展SupportMapFragment并使用自定义视图,例如这样:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp">
<com.google.android.gms.maps.MapView
android:id="@+id/map_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
map:cameraBearing="0"
map:cameraTargetLat="54.25"
map:cameraTargetLng="-4.56"
map:cameraTilt="30"
map:cameraZoom="5.6"
map:mapType="normal"
map:uiCompass="true"
map:uiRotateGestures="true"
map:uiScrollGestures="true"
map:uiTiltGestures="true"
map:uiZoomControls="false"
map:uiZoomGestures="true">
</com.google.android.gms.maps.MapView>
</LinearLayout>
如果没有任何投诉,那么生成的地图就是正确的。然而,对我来说,这还引发了更多的问题,因为没有好的示例说明如何进行子类化,您必须做更多的工作来覆盖onCreateView,当我随后尝试对地图进行任何操作时,我会得到以下结果:
java.lang.IllegalStateException: Map size should not be 0. Most likely, layout has not yet occured for the map view.
即使在地图出现后等待30秒钟(仅限第一次加载),也无法解决问题。