Android中的Google地图碎片在XML中。我收到“意外的命名空间前缀”错误提示。

35
我正在尝试学习安卓,按照使用Google Maps API V.2的说明操作后,我现在已经成功了。然而,在developers.google.com找到的有关配置地图初始状态的说明建议在xml文件中定义一个名称空间,例如“map”。
下面的xml代码给我带来了错误提示:“意外的名称空间前缀'map'”。在片段标签内定义给出了相同的错误,但是将“xmlns”替换为“map”。
显然我在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>

2
你应该清理项目,有时候Eclipse会出现一些愚蠢的错误。 - The Finest Artist
9个回答

26

我也遇到过这个问题。我执行了 Project/Clean 操作,错误消失了,现在它可以正常工作。


7
我只用Eclipse开发Android,我从未理解为什么它不会自动清理。清理只有在你再次编辑XML文件之前有效,然后问题又会出现。 - Rob
@Rob,你解决了每次都出错的问题吗?我总是不得不一直清理。 - Snake
@Snake 很抱歉,我最近没有使用过 Google 地图 API,所以无法提供帮助。不过我已经转而使用 Android Studio 了。 - Rob

20

您需要做两件事:

第一:

将 Google Play 服务的依赖项添加到您的项目中: https://docs.google.com/document/pub?id=19nQzvKP-CVLd7_VrpwnHfl-AE9fjbJySowONZZtNHzw

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

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

选择Project > Properties,选择Java Build Path,进入Libraries。选择Add External Jars,包含以下jar文件,并单击“确定”:

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

现在我的项目再也没有错误了 :)


1
这是正确的解决方案。请注意,对于Android Studio,请将google-play-services_lib添加为模块依赖项。 - Sofi Software LLC

19

今天我遇到了同样的问题。昨晚我升级了SDK,之前没有看到这个问题。我也加载了Android Map V2示例演示项目,今天“multimap_demo.xml”文件显示“在片段标签中找到意外的命名空间前缀“map””错误。我应用了建议的xml包含方式,现在它又可以工作了。会给它一个+1,但没有得到信用。

更新: 我忘记了这个问题,今天重新编写了我的代码并删除了包含内容。当然,错误又出现了。我在片段标记中添加了以下内容:

tools:ignore="MissingPrefix"

它似乎至少可以掩盖这个问题。

更新:这个 bug 显然是由于 Android Lint 工具中的一个 bug 导致的。请参考问题 https://code.google.com/p/gmaps-api-issues/issues/detail?id=5002


13

还有另一种解决方法,让您继续在布局文件中设置所有内容,而不是在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"/>

我的_超棒_布局.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>

我仍然持续收到以下错误信息:“错误:在包{packagename}中找不到属性{xmlParam}的资源标识符”。奇怪的是,我记得3-4个月前将可运行的代码提交到我的代码库,但几天前尝试编译时,却一直遇到和原帖作者一样的错误。 - Indrek Kõue

4

我知道,这并不是名称空间问题的解决方案,但或许可以有所帮助。

由于我不懂任何XML解决方案,我通过编程实现了它:

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地图默认已初始化,但随后从代码中直接获取其参数。


3
我遇到了完全相同的问题。所提供的示例为:
<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秒钟(仅限第一次加载),也无法解决问题。


我们似乎确实坐在同一条船上。我开始用Java代码编写。感谢您的回复,我会尝试使用自定义视图。 - Alex
有趣的是,如果您定义任何其他命名空间,它们似乎都可以工作。例如,我需要为我的Context使用xmlns:tools="http://schemas.android.com/tools"。 - Tobias Reich

1
我认为你不能像使用<!-- Definition -->那样在标签内部放置XML注释。如果将其删除,问题是否仍然存在?

是的,我后来添加了注释以尝试解释问题。我还没有尝试过带有注释的代码,你可能是对的。不过还是谢谢你抽出时间来看! - Alex

0
在我的情况下,我犯了一个大错误,忘记在gradle文件中添加我的谷歌地图依赖项:
compile 'com.google.android.gms:play-services-maps:11.2.0'

0

显然这只是一个误导性的Lint检查错误。您可以在Eclipse的“问题视图”中右键单击带有错误的行,选择“快速修复”选项,然后选择例如“忽略项目检查”来删除它。

错误消失了,项目构建成功,应用程序也能够完美运行。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接