2

I'm getting an error:

Unexpected namespace prefix "xmlns" found for tag fragment

for lines

 <fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"

in

  <?xml version="1.0" encoding="utf-8"?>
        <TabHost android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@android:id/tabhost"
        xmlns:android="http://schemas.android.com/apk/res/android"
        >
        <TabWidget
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@android:id/tabs"
        />
         <FrameLayout
         android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@android:id/tabcontent"
         >
         <fragment xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/item_list"
        android:name="com.example.storeitemfinder.ItemListFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        tools:context=".ItemListActivity"
        tools:layout="@android:layout/list_content" />
         </FrameLayout>
        </TabHost>

Does anyone know what's wrong?

flx
  • 14,146
  • 11
  • 55
  • 70
user2749364
  • 23
  • 2
  • 7
  • possible duplicate of [Unexpected namespace prefix "xmlns" found for tag LinearLayout](http://stackoverflow.com/questions/14916638/unexpected-namespace-prefix-xmlns-found-for-tag-linearlayout) – Louis Jan 25 '14 at 11:55

4 Answers4

1

Remove this

 xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"

for fragment in xml

http://developer.android.com/guide/topics/resources/layout-resource.html

The root element can be either a ViewGroup, a View, or a element, but there must be only one root element and it must contain the xmlns:android attribute with the android namespace as shown.

To make sure i just went through the below posts that are similar

Unexpected namespace prefix "xmlns" for tag fragment

Unexpected namespace prefix "xmlns" found for tag LinearLayout

Unexpected namespace prefix "xmlns" found for tag ListView

Edit:

From the link you posted in your comment it looks like you are looking for is tabs with fragments. So try the below and modify according to your requirements.

Example :

I have set min sdk is 11

MainActivity.java

public class MainActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        final ActionBar actionBar = getActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        Tab tabA = actionBar.newTab();
        tabA.setText("Tab A");
        tabA.setTabListener(new TabListener<MyFragmentA>(this, "Tag A", MyFragmentA.class));
        actionBar.addTab(tabA);

        Tab tabB = actionBar.newTab();
        tabB.setText("Tab B");
        tabB.setTabListener(new TabListener<MyFragmentB>(this, "Tag B", MyFragmentB.class));
        actionBar.addTab(tabB);

        Tab tabC = actionBar.newTab();
        tabC.setText("Tab C");
        tabC.setTabListener(new TabListener<MyFragmentC>(this, "Tag C", MyFragmentC.class));
        actionBar.addTab(tabC);

        if (savedInstanceState != null) {
            int savedIndex = savedInstanceState.getInt("SAVED_INDEX");
            getActionBar().setSelectedNavigationItem(savedIndex);
        }

    }

 @Override
 protected void onSaveInstanceState(Bundle outState) {
  // TODO Auto-generated method stub
  super.onSaveInstanceState(outState);
  outState.putInt("SAVED_INDEX", getActionBar().getSelectedNavigationIndex());
 }

 public static class TabListener<T extends Fragment> 
     implements ActionBar.TabListener{

        private final Activity myActivity;
        private final String myTag;
        private final Class<T> myClass;

        public TabListener(Activity activity, String tag, Class<T> cls) {
            myActivity = activity;
            myTag = tag;
            myClass = cls;
        }

  @Override
  public void onTabSelected(Tab tab, FragmentTransaction ft) {

   Fragment myFragment = myActivity.getFragmentManager().findFragmentByTag(myTag);

   // Check if the fragment is already initialized
         if (myFragment == null) {
             // If not, instantiate and add it to the activity
             myFragment = Fragment.instantiate(myActivity, myClass.getName());
             ft.add(android.R.id.content, myFragment, myTag);
         } else {
             // If it exists, simply attach it in order to show it
             ft.attach(myFragment);
         }

  }

  @Override
  public void onTabUnselected(Tab tab, FragmentTransaction ft) {

   Fragment myFragment = myActivity.getFragmentManager().findFragmentByTag(myTag);

   if (myFragment != null) {
             // Detach the fragment, because another one is being attached
             ft.detach(myFragment);
         }

  }

  @Override
  public void onTabReselected(Tab tab, FragmentTransaction ft) {
   // TODO Auto-generated method stub

  }

    }
}

Framgent A

public class MyFragmentA extends Fragment {

 @Override
 public View onCreateView(LayoutInflater inflater, ViewGroup container,
   Bundle savedInstanceState) {
  View myFragmentView = inflater.inflate(R.layout.fragment_a, container, false);
  return myFragmentView;
 }

}

Fragment B

public class MyFragmentB extends Fragment {

 @Override
 public View onCreateView(LayoutInflater inflater, ViewGroup container,
   Bundle savedInstanceState) {
  View myFragmentView = inflater.inflate(R.layout.fragment_b, container, false);
  return myFragmentView;
 }

}

Fragment C

public class MyFragmentC extends Fragment {

 @Override
 public View onCreateView(LayoutInflater inflater, ViewGroup container,
   Bundle savedInstanceState) {
  View myFragmentView = inflater.inflate(R.layout.fragment_c, container, false);
  return myFragmentView;
 }

}

fragment A

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:orientation="vertical" >
   <TextView
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:text="It's Fragment A" />
   <ImageView
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:src="@drawable/ic_launcher"/>
</LinearLayout>

fragment b xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:orientation="vertical" >
   <TextView
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:text="It's Fragment B" />
   <ImageView
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:scaleType="center"
       android:src="@drawable/ic_launcher"/>
</LinearLayout>

fragment c xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:orientation="vertical" >
   <TextView
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:text="It's Fragment C" />

</LinearLayout>

Snap shot

enter image description here

Community
  • 1
  • 1
Raghunandan
  • 132,755
  • 26
  • 225
  • 256
  • thanks, but that just removes those two errors and gives me three more: error: Error parsing XML: unbound prefix for fragment Attribute is missing the Android namespace prefix for the last two :( – user2749364 Sep 08 '13 at 01:53
  • 1) error: Error parsing XML: unbound prefix for – user2749364 Sep 08 '13 at 01:55
  • @user2749364 remove this tools:context=".ItemListActivity" and tools:layout="@android:layout/list_content" / – Raghunandan Sep 08 '13 at 01:57
  • hrm... that seemed to remove the errors, but when i run the program on my phone i still only have 1 tab! – user2749364 Sep 08 '13 at 02:00
  • @user2749364 you need to have tabs. you do not have one. – Raghunandan Sep 08 '13 at 02:01
  • well i was trying to make my app have two tabs -_- basically i was trying to embed the master-flow default template into one tab, and then create a second tab for another activity – user2749364 Sep 08 '13 at 02:02
  • I was trying to follow this : http://www.codeproject.com/Articles/107693/Tabbed-Applications-in-Android and frankenstein that code into something that could hold my master-flow code in one tab . could you help me do that please? i've been stuck on this for hours :-/ – user2749364 Sep 08 '13 at 02:03
  • He needs to *add* something, not keep removing things. You seem to be just guessing. – user207421 Sep 08 '13 at 02:03
  • well when i run that code it is error free.. so thanks for that. but it's basically a default masterflow template – user2749364 Sep 08 '13 at 02:05
  • oh maybe it's because i'm missing a linear layout chunk below my fragment and before the end of framelayout – user2749364 Sep 08 '13 at 02:12
  • @user2749364 check the example here http://developer.android.com/reference/android/app/TabActivity.html – Raghunandan Sep 08 '13 at 02:15
  • @user2749364 removing those lines in fragment is right i guess. but your need to add tabs to the tab host. http://www.codeproject.com/Articles/107693/Tabbed-Applications-in-Android. check the java code and the xml in the same link you posted. – Raghunandan Sep 08 '13 at 07:51
0

You need an xmlns= schema attribute on the document element, and probably an xmlns:xsi attribute as well, for example:

xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

[This is from a web.xml, the xmlns here doesn't apply to your namespace.]

user207421
  • 305,947
  • 44
  • 307
  • 483
  • how do i do that? where do i put it? – user2749364 Sep 08 '13 at 02:06
  • @EJP but those lines xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools should be there only for root element i guess. Having those lines is a problem? – Raghunandan Sep 08 '13 at 02:12
  • That's not what I'm talking about. You need an "xmlns=" attribute as well. The attributes you mention can be anywhere. – user207421 Sep 08 '13 at 03:02
  • @EJP i tried it as you said but it does give error as op stated. i wonder if its android specific http://developer.android.com/reference/javax/xml/namespace/NamespaceContext.html. I also searched on stackvoerflow and some say it should be for the root element only. – Raghunandan Sep 08 '13 at 07:33
  • @EJP and also this http://developer.android.com/guide/topics/resources/layout-resource.html. quoting from docs The root element can be either a ViewGroup, a View, or a element, **but there must be only one root element and it must contain the xmlns:android attribute with the android namespace as shown**. – Raghunandan Sep 08 '13 at 07:39
  • I haven't said otherwise. I've said something else, and I *specifically* said 'as well'. Don't try to manufacture a disagreement about something I haven't even said. – user207421 Sep 08 '13 at 07:53
  • @EJP i am not disagreeing i am just merely quoting it from the docs. You don't need those two line i asked op to remove fro fragment element. It is necessary fro the root element. the root element must have xmlnx:android attribute with android name space. Also the examples in the docs don't have those for child elements. Tell me if i am wrong. if i am wrong i will make a correction and makes sure i learn from my mistakes – Raghunandan Sep 08 '13 at 07:56
  • I haven't said you're wrong here. I haven't said anything about it at all. You are arguing to no purpose. Or are you discussing your *own* answer here, rather than mine? If so, you are doing it in the wrong place. This part is supposed to be for comments on *my* answer. – user207421 Sep 08 '13 at 08:01
0

In you tabhost layout file, change the parent TabHost Tag code as below

 <TabHost   xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@android:id/tabhost"
        >

xmlns declarations have to be first and then it has to be used. as far as I am concern defining xmlns second time in the layout file doesn't affect or create error but I hope you are facing problem due to wrong declaration

TNR
  • 5,839
  • 3
  • 33
  • 62
0

This may or may not be applicable to your specific problem. I ran into the same issue. Eclipse at times can be rather fussy. What I did was: Backed up the original file. Then did a copy paste back into the code that was causing the error. Recompiled and the error was gone.

DoesEatOats
  • 625
  • 7
  • 13