35

I'm trying to learn android, and having followed the instructions on how to use the Google Maps API V.2 I now got it working. However, the instructions on how to configure the initial state of the maps, found at developers.google.com, suggests a namespace defined in the xml-file, in this case "map".

The xml-code below gives med the error "Unexpected namespace prefix "map"". Trying to define the xmlns:map inside the fragment tag gave the same error but with "xmlns".

I'm obviously missing some fundamental xml-knowledge here, can someone help me out?

<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>
Alex
  • 365
  • 1
  • 3
  • 5

9 Answers9

26

I had this problem as well. I did Project/Clean and the error went away and it works fine now.

Kent
  • 1,691
  • 4
  • 19
  • 27
  • 7
    I only use eclipse for Android, I've never understood why it doesn't does clean up after itself automatically. Cleaning only works until you edit the XML file again then the problem recurs – Rob Jul 18 '13 at 13:09
  • @Rob did you figure out how to not get the error everytime. I have to clean all the time – Snake Nov 28 '14 at 05:10
  • @Snake unfortunately not, I haven't done much work with google maps api of late so I can't really help, sorry. I did however switch to using Android Studio – Rob Nov 28 '14 at 15:03
20

You have to do two things:

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

Add the dependency to Google Play Services into your project

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

Second: https://developers.google.com/maps/documentation/android/intro

Select Project > Properties, select Java Build Path, and navigate to Libraries. Select Add External Jars, include the following jar files, and click OK:

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

Now my project shows no errors anymore :)

nbro
  • 15,395
  • 32
  • 113
  • 196
marcel
  • 412
  • 2
  • 10
19

I have the same problem today. I upgraded the SDK last night and did not see this problem before. I had the Android Map V2 sample demo project loaded too and today the "multimap_demo.xml" file is showing the "Unexpected namespace prefix "map" found for tag fragment" error. I applied the xml include suggested and it is working again. Would give it a +1 but got no cred.

UPDATE: I forgot about this problem and reworked my code today and removed the include. Of course the error came back. I found this and added it to the layout in the fragment stanza:

tools:ignore="MissingPrefix"

It seems to at least mask the problem.

Update: This bug apparently happens due to a bug in Android Lint Tool. Refer issue https://code.google.com/p/gmaps-api-issues/issues/detail?id=5002

codemonger
  • 47
  • 8
WigglyWorld
  • 705
  • 6
  • 13
13

There is another workaround that lets you continue to set everything up in layout files instead of in the Java code. Since the error only seems to happen when the SupportMapFragment is a child of a ViewGroup in the layout file, one can extract the <fragment> element into its own layout file and then just include it in the desired larger layout.

For example, given that you are trying to do this:

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>

You could instead break it up like so:

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>
Scott W
  • 9,742
  • 2
  • 38
  • 53
  • I still keep receiving: 'error: No resource identifier found for attribute {xmlParam} in package {packagename}'. Weird thing is that, I remember committing working code to my repo 3-4 months ago and when I tried to compile it few days ago, I kept getting the same error as original poster. – Indrek Kõue Apr 19 '13 at 22:26
4

Well, I know, this isn't really a solution for the name space problem, maybe this might help.

Since I don't know any XML solution, I did it programmatically:

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);
}

So the Google Map is initialized default but gets its parameters directly after that from the code.

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
Tobias Reich
  • 4,952
  • 3
  • 47
  • 90
3

I've got exactly the same problem. The provided example

<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"/>

works fine, but if you try to add it into a parent element it refuses to accept the xmlns. If you move the xmlns declaration to the top element it still refuses to accept the map prefix in the fragment:

Unexpected namespace prefix "map" found for tag fragment

Now if you extend SupportMapFragment and use a custom view such as this:

<?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>

...then it doesn't complain and the resultant map is correct. For me that raises further problems however as there are no decent examples of how to do this subclassing, you have to do more than override onCreateView and when I try to do anything to the map subsequently I get the following:

java.lang.IllegalStateException: Map size should not be 0. Most likely, layout has not yet occured for the map view.

...even if I wait 30 seconds after the map has appeared.(only first loading)

Dinithe Pieris
  • 1,822
  • 3
  • 32
  • 44
colesadam
  • 81
  • 1
  • 6
  • We seem indeed to be sitting in the same boat. I moved on to doing it in the java-code. Thanks for the response, I will try out the custom view. – Alex Feb 24 '13 at 19:55
  • And funny enough, if you define any other namespace they seem to work. Like I needed xmlns:tools="http://schemas.android.com/tools" for my Context. – Tobias Reich Feb 27 '13 at 15:51
1

I don't think you can put XML comments inside a tag like you are doing with <!-- Definition -->. If you remove that does the problem still occur?

Matthew
  • 6,356
  • 9
  • 47
  • 59
  • Yes, I added the comments afterwards in order to try and explain the problem. I haven't tried the code with the comments in it, you may be right. Thank you for taking your time though! – Alex Feb 24 '13 at 19:53
0

Obviously this is just a mis-lead Lint check error. You can remove it when, in Eclipse's Problem view, you right-click the line with the error, select the Quick fix option and select e.g. Ignore Check for project.

The error goes away, the project builds and the app runs perfectly well.

Ridcully
  • 23,362
  • 7
  • 71
  • 86
0

In my case a big miss, I forget to add in the gradle file my google map dependency:

compile 'com.google.android.gms:play-services-maps:11.2.0'
Pablo Cegarra
  • 20,955
  • 12
  • 92
  • 110