为标记片段找到了意外的名称空间前缀“xmlns”

时间:2013-09-08 01:39:07

标签: android xml android-layout

我收到了一个错误:

为标记片段

找到了意外的名称空间前缀“xmlns”

代表行

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

有谁知道什么是错的?

4 个答案:

答案 0 :(得分:1)

删除此

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

表示xml中的片段

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

根元素可以是ViewGroup,View或元素,但必须只有一个根元素,并且必须包含带有android命名空间的xmlns:android属性,如图所示。

为了确保我刚刚浏览了类似的以下帖子

Unexpected namespace prefix "xmlns" for tag fragment

Unexpected namespace prefix "xmlns" found for tag LinearLayout

Unexpected namespace prefix "xmlns" found for tag ListView

编辑:

从您在评论中发布的链接看起来您正在寻找带有片段的标签。因此,请尝试以下内容并根据您的要求进行修改。

示例:

我设置min sdk是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;
 }

}

片段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;
 }

}

片段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;
 }

}

片段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>

片段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>

片段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>

快照

enter image description here

答案 1 :(得分:0)

您需要在文档元素上使用xmlns=架构属性,并且可能还需要xmlns:xsi属性,例如:

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

[这是来自web.xml,xmlns此处不适用于您的命名空间。]

答案 2 :(得分:0)

在tabhost布局文件中,更改父TabHost标记代码,如下所示

 <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声明必须是第一个然后必须使用它。至于我担心在布局文件中第二次定义xmlns不会影响或创建错误但我希望你因错误的声明而面临问题

答案 3 :(得分:0)

这可能适用于您的具体问题,也可能不适用。我遇到了同样的问题。 Eclipse有时可能比较挑剔。 我做的是: 备份原始文件。然后复制粘贴回导致错误的代码。 重新编译,错误消失了。