xml中的Android Google Maps片段.我得到"意外的命名空间前缀"

Ale*_*lex 34 xml android xml-parsing android-layout google-maps-android-api-2

我正在尝试学习android,并按照如何使用Google Maps API V.2的说明进行操作.但是,有关如何配置地图初始状态的说明(在developers.google.com中找到)建议在xml文件中定义名称空间,在本例中为"map".

下面的xml代码给出了错误"Unexpected namespace prefix"map" ".尝试定义片段标记内的xmlns:map会产生相同的错误,但使用"xmlns".

我显然在这里缺少一些基本的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>
Run Code Online (Sandbox Code Playgroud)

Ken*_*ent 26

我也有这个问题.我做了Project/Clean,错误消失了,现在工作正常.

  • 我只使用Android的eclipse,我从来没有理解为什么它不能自动清理它.清理仅在您再次编辑XML文件之后才有效,然后问题再次出现 (7认同)

mar*_*cel 20

你必须做两件事:

首先:https: //docs.google.com/document/pub?id = 19nQzvKP-CVLd7_VrpwnHfl-AE9fjbJySowONZZtNHzw

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

Project -> Properties -> Android -> Library, Add -> google-play-services_lib
Run Code Online (Sandbox Code Playgroud)

第二: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
Run Code Online (Sandbox Code Playgroud)

现在我的项目不再显示错误了:)


Wig*_*rld 19

我今天也有同样的问题.我昨晚升级了SDK,之前没有看到这个问题.我也加载了Android Map V2示例项目,今天"multimap_demo.xml"文件显示"找不到标记片段"错误的"Unexpected namespace prefix"映射.我应用了xml include建议,它再次工作.会给它+1,但没有信誉.

更新:我忘记了这个问题,今天重新编写了我的代码并删除了包含.当然错误又回来了.我找到了这个并将其添加到片段节中的布局中:

tools:ignore="MissingPrefix"
Run Code Online (Sandbox Code Playgroud)

它似乎至少掩盖了这个问题.

更新:由于Android Lint Tool中的错误,显然会发生此错误.请参阅问题https://code.google.com/p/gmaps-api-issues/issues/detail?id=5002


Sco*_*t W 13

还有另一种解决方法可以让您继续在布局文件中而不是在Java代码中设置所有内容.由于错误似乎只发生在布局文件中SupportMapFragmenta的子元素时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>
Run Code Online (Sandbox Code Playgroud)

你可以这样拆分它:

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"/>
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)