2

so I'm new to android development and i was following a course but when i wanted to run the program i had an error which says: error parsing XML:unbound prefix in line 2 in the code below (this code is in activity_main) i tried many solution from previously answered questions but didn't work

<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
app:srcCompat="@android:drawable/ic_dialog_email" />


and here what i have in layout>content_main

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/activity_main">

<LinearLayout
xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">>
<EditText
android:layout_width ="match_parent"
android:layout_height="wrap_content" />

<Button
    android:layout_width ="match_parent"
    android:layout_height="wrap_content"
    android:text ="CALCULATE" />

<LinearLayout
    android:layout_width ="match_parent"
    android:layout_height="wrap_content">
<TextView
        android:text ="Tip"
        android:layout_width ="wrap_content"
        android:layout_height="wrap_content" />
<TextView
        android:layout_width ="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

<LinearLayout
    android:layout_width ="match_parent"
    android:layout_height="wrap_content">
<TextView
        android:text ="Total"
        android:layout_width ="wrap_content"
        android:layout_height="match_parent" />
<TextView
        android:layout_width ="wrap_content"
        android:layout_height="match_parent" />
</LinearLayout>
</LinearLayout>

</RelativeLayout>
Ourania
  • 21
  • 3
  • 3
    Possible duplicate of [frequent issues arising in android view, Error parsing XML: unbound prefix](https://stackoverflow.com/questions/2221221/frequent-issues-arising-in-android-view-error-parsing-xml-unbound-prefix) – mjwills Jan 30 '19 at 20:47
  • `i tried many solution from previously answered questions but didn't work` please indicate at least one such answer, what you tried and how `didn't work` manifested itself. – greybeard Jan 31 '19 at 00:01

1 Answers1

2

So what the unbound prefix error is telling you is that you are using a prefix in your xml file that is not recognized.

By prefix, it is referring to the value the comes before the colon. For example:

<Button
    android:layout_width ="match_parent"
    android:layout_height="wrap_content"
    android:text ="CALCULATE" />

In the snippet above, android is the prefix.

The way you tell the XML file which prefix values are supported are by providing XML Namespaces. You do this using the xmlns attribute:

<RelativeLayout 
    xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:showIn="@layout/activity_main">

In the code snippet above, there are 4 prefixes that have been defined: ads, android, app, and tools.

You only need to define these prefixes once on the root (top-most) element. Then all other nested/child elements can use the prefix. Your XML file should only have one root element. Everything else should be inside this root.

For the xml posted above there are two issues I see:

  1. Your Floating Action Button needs to be nested inside the root layout
  2. You only need to define the XML Namespaces once at the root. It looks like it is defined twice, once on the RelativeLayout and once on the LinearLayout.

Try modifying your layout to look like this:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
    xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:showIn="@layout/activity_main">
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">>
        <EditText
            android:layout_width ="match_parent"
            android:layout_height="wrap_content" />
        <Button
            android:layout_width ="match_parent"
            android:layout_height="wrap_content"
            android:text ="CALCULATE" />

        <LinearLayout
            android:layout_width ="match_parent"
            android:layout_height="wrap_content">
            <TextView
                    android:text ="Tip"
                    android:layout_width ="wrap_content"
                    android:layout_height="wrap_content" />
            <TextView
                    android:layout_width ="match_parent"
                    android:layout_height="wrap_content" />
        </LinearLayout>

        <LinearLayout
            android:layout_width ="match_parent"
            android:layout_height="wrap_content">
            <TextView
                    android:text ="Total"
                    android:layout_width ="wrap_content"
                    android:layout_height="match_parent" />
            <TextView
                    android:layout_width ="wrap_content"
                    android:layout_height="match_parent" />
        </LinearLayout>
    </LinearLayout>
    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        app:srcCompat="@android:drawable/ic_dialog_email" />
</RelativeLayout>

In the xml above, I made the following changes:

  1. There is only one root layout (RelativeLayout) and everything else, including the FloatingActionButton, is nested inside this root
  2. The XML Namespaces are only defined once as the first attributes on the root. They must be defined before they can be used!

Hope it helps!

pnavk
  • 4,552
  • 2
  • 19
  • 43
  • i did the same thing but stll give me the same error i don't know what's the problem – Ourania Feb 05 '19 at 17:55
  • Plus in the designer view it says "this project contains resources that were not compiled succsesfully, rendring may be affected" – Ourania Feb 05 '19 at 17:57