Android谷歌地图片段在xml中。我得到;意外的命名空间前缀";

Android谷歌地图片段在xml中。我得到;意外的命名空间前缀";,android,xml,android-layout,xml-parsing,google-maps-android-api-2,Android,Xml,Android Layout,Xml Parsing,Google Maps Android Api 2,我正在尝试学习android,在遵循了关于如何使用Google Maps API V.2的说明后,我现在已经可以使用了。 但是,有关如何配置映射的初始状态的说明(位于)建议在xml文件中定义一个名称空间,在本例中为“映射” 下面的xml代码为med提供了错误“意外的命名空间前缀”map“”。尝试在片段标记内定义xmlns:map时出现了相同的错误,但使用了“xmlns” 我显然缺少一些基本的xml知识,有人能帮我吗 <RelativeLayout xmlns:android="h

我正在尝试学习android,在遵循了关于如何使用Google Maps API V.2的说明后,我现在已经可以使用了。 但是,有关如何配置映射的初始状态的说明(位于)建议在xml文件中定义一个名称空间,在本例中为“映射”

下面的xml代码为med提供了错误“意外的命名空间前缀”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>


我认为您不能像使用
那样将XML注释放入标记中。如果删除该选项,问题是否仍然存在?

我遇到了完全相同的问题。提供的示例

<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"/>
现在,如果您扩展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>

…即使我在地图出现后等待30秒。(仅第一次加载)

我知道,这并不是解决名称空间问题的真正方法,也许这会有所帮助

因为我不知道任何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地图默认初始化,但直接从代码中获取参数。

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

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

my\u awesome\u 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>
<?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"/>
...
<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>
。。。
你可以这样把它分解:

包含\u map\u fragment.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>
<?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"/>
...
<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>

my\u awesome\u 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>
<?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"/>
...
<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>
。。。

我今天也有同样的问题。我昨晚升级了SDK,以前没有看到这个问题。我也加载了Android Map V2示例演示项目,今天“multimap_demo.xml”文件显示了“为标记片段找到的意外名称空间前缀”映射”错误。我应用了xml include建议,它又开始工作了。会给它一个+1,但没有信用

更新: 我忘记了这个问题,今天修改了代码并删除了include。当然,错误又回来了。我发现了这一点,并将其添加到片段节的布局中:

tools:ignore="MissingPrefix"
这似乎至少掩盖了这个问题


更新:这个错误显然是由于Android Lint工具中的一个错误造成的。参考问题你必须做两件事:

第一:

将Google Play服务的依赖项添加到项目中

Project -> Properties -> Android -> Library, Add -> google-play-services_lib
第二:

选择
Project>Properties
,选择
Java构建路径
,然后导航到
。 选择添加外部jar,包括以下jar文件,然后单击确定:

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

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

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

显然,这只是一个错误的引导皮棉检查错误。在Eclipse的问题视图中,右键单击出现错误的行,选择“快速修复”选项,然后选择“忽略检查项目”,就可以删除它


错误消失了,项目构建和应用程序运行都很好。

在我的例子中,一个重大失误,我忘记在gradle文件中添加我的谷歌地图依赖项:

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

是的,为了尝试解释问题,我后来添加了评论。我还没有尝试过带有注释的代码,你可能是对的。谢谢你花时间!我们似乎确实坐在同一条船上。我接着用java代码来做这件事。感谢您的回复,我将尝试自定义视图。有趣的是,如果您定义任何其他名称空间,它们似乎可以工作。就像我需要xmlns:tools=”“作为我的上下文。你应该清理项目,有时候eclipse会犯愚蠢的错误。我仍然收到:“错误:在包{packagename}中找不到属性{xmlParam}的资源标识符”。奇怪的是,我记得3-4个月前,我将工作代码提交给了我的repo,几天前,当我试图编译它时,我不断得到与原始海报相同的错误。这是正确的解决方案。请注意,对于Android Studio,添加google-play-services_lib作为模块依赖项。我只在Android上使用eclipse,我一直不明白为什么它不会自动清理。只有在再次编辑XML文件之后,才能进行清理,然后问题才会解决recurs@Rob你有没有想过如何避免每次都出错。我必须打扫所有的房间time@Snake不幸的是,我没有做太多的工作与谷歌地图api的最近,所以我不能真正帮助,对不起。不过,我确实改用了Android Studio