Android的Google地图片段在xml中。 我得到“意外的命名空间前缀”

我正在尝试学习android,并按照关于如何使用Google Maps API V.2的说明进行操作。 不过,有关如何在developers.google.com上find映射初始状态的说明,build议在xml文件中定义一个名称空间,在本例中为“map”。

下面的xml代码给出错误“意外的命名空间前缀”地图“ ”。 试图在fragment标签中定义xmlns:map给出了与“xmlns”相同的错误。

我明显缺less一些基本的XML知识在这里,有人可以帮我吗?

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:map="http://schemas.android.com/apk/res-auto" <!-- Definition --> android:layout_width="match_parent" android:layout_height="match_parent"> <fragment 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"/> <!-- PROBLEM --> </RelativeLayout> 

你必须做两件事情:

第一个: https : //docs.google.com/document/pub?id = 19nQzvKP-CVLd7_VrpwnHfl-AE9fjbJySowONZZtNHzw

将依赖关系添加到Google Play服务中

 Project -> Properties -> Android -> Library, Add -> google-play-services_lib 

第二: https : //developers.google.com/maps/documentation/android/intro

selectProject> Properties,selectJava Build Path,然后导航到Libraries。 selectAdd External Jars,包含以下jar文件,然后单击OK:

 <android-sdk-folder>/extras/android/compatibility/v4/android-support-v4.jar 

现在我的项目显示没有错误:)问候,马塞尔

我也有这个问题。 我做了项目/清理和错误消失,现在它工作正常。

我今天有同样的问题。 我昨晚升级了SDK,之前没有看到这个问题。 我也加载了Android Map V2示例演示项目,而今天“multimap_demo.xml”文件显示了“为标签片段find的”意外命名空间前缀“映射”错误。 我申请了XML包含build议,它再次工作。 会给它一个+1,但没有信誉。

更新:我忘了这个问题,并重新编写了我的代码,并删除了包含。 当然,错误回来了。 我发现这一点,并将其添加到片段中的布局:

 tools:ignore="MissingPrefix" 

这似乎至less掩盖了这个问题。

更新:这个错误显然是由于Android Lint Tool中的一个错误而发生的。 请参阅https://code.google.com/p/gmaps-api-issues/issues/detail?id=5002

还有另一个解决方法,可以让您继续在布局文件中而不是在Java代码中设置所有内容。 由于只有在SupportMapFragment是布局文件中的ViewGroup的子元素时才会发生该错误,因此可以将<fragment>元素提取到其自己的布局文件中,然后将其包含在所需的较大布局中。

例如,假设你正在尝试这样做:

my_awesome_layout.xml

 ... <RelativeLayout 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"> <fragment 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"/> </RelativeLayout> 

你可以把它分解为:

include_map_fragment.xml

 <?xml version="1.0" encoding="utf-8"?> <fragment xmlns:android="http://schemas.android.com/apk/res/android" xmlns:map="http://scheams.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"/> 

my_awesome_layout.xml

 ... <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <include android:id="@+id/map" android:layout_width="match_parent" android:layout_height="match_parent" layout="@layout/include_map_fragment" /> </RelativeLayout> 

那么,我知道,这不是名称空间问题的解决scheme,也许这可能有帮助。

由于我不知道任何XML解决scheme,我以编程方式:

 mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap(); mMap.setTrafficEnabled(true); setupMapView(); private void setupMapView(){ UiSettings settings = mMap.getUiSettings(); mMap.animateCamera(CameraUpdateFactory.newCameraPosition( new CameraPosition(new LatLng(50.0, 10.5), 13.5f, 30f, 112.5f))); // zoom, tilt, bearing mMap.setTrafficEnabled(true); settings.setAllGesturesEnabled(true); settings.setCompassEnabled(true); settings.setMyLocationButtonEnabled(true); settings.setRotateGesturesEnabled(true); settings.setScrollGesturesEnabled(true); settings.setTiltGesturesEnabled(true); settings.setZoomControlsEnabled(true); settings.setZoomGesturesEnabled(true); } 

所以Google Map默认是初始化的,但是直接从代码中获取参数。

我有完全相同的问题。 提供的例子

 <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声明移动到顶层元素,它仍然拒绝接受片段中的映射前缀:

 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秒(只有第一次加载)

我不认为你可以像在<!-- Definition -->那样把XML注释放在标签 。 如果你删除,问题是否仍然存在?

很明显,这只是一个误导林特检查错误。 在Eclipse的“ 问题”视图中 ,右键单击带有错误的行,select“ 快速修复”选项,然后select“ 忽略检查项目” ,可以将其删除。

错误消失,项目build立和应用程序运行良好。

在我的情况下,一个大错过,我忘了添加在我的谷歌地图依赖的gradle文件:

 compile 'com.google.android.gms:play-services-maps:11.2.0'