Android 意外的命名空间前缀";xmlns";为标记片段找到

Android 意外的命名空间前缀";xmlns";为标记片段找到,android,xml,android-layout,Android,Xml,Android Layout,我得到一个错误: 为标记片段找到意外的命名空间前缀“xmlns” 用于线路 <fragment xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" 删除此 xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schema

我得到一个错误:

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

用于线路

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

 xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
用于xml中的片段

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

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

编辑:

从您在评论中发布的链接来看,您似乎正在查找带有片段的is选项卡。因此,请尝试下面的方法,并根据您的要求进行修改

例如:

我已将最小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

  }

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

快照


在文档元素上需要一个
xmlns=
schema属性,可能还需要一个
xmlns:xsi
属性,例如:

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

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

在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不会影响或产生错误,但我希望您会因为错误的声明而面临问题,这可能适用于您的特定问题,也可能不适用于您的特定问题。我遇到了同样的问题。 日食有时会很麻烦。 我所做的是: 备份了原始文件。然后将复制粘贴回导致错误的代码中。
重新编译后错误消失。

谢谢,但这只是删除了这两个错误,并给了我三个错误:错误:解析XML时出错:fragment属性的未绑定前缀缺少最后两个的Android命名空间前缀:(1)错误:解析XML时出错:@user2749364的未绑定前缀删除此工具:context=“.ItemListActivity”和工具:layout=“@android:layout/list_content”/hrm。。。这似乎消除了错误,但当我在手机上运行程序时,我仍然只有一个标签@user2749364您需要有选项卡。你没有,我怎么做?我应该把它放在哪里?@EJP但是那些行xmlns:android=”“xmlns:tools=“应该只放在根元素上。有那些行是个问题吗?我不是说这个。你需要一个“xmlns”=“属性也是。您提到的属性可以在任何地方。@EJP我按您所说的那样尝试过,但它确实给出了op所说的错误。我想知道它是否是特定于android的。我还搜索了stackvoerflow,有人说它应该只针对根元素。@EJP和这个。从文档中引用根元素可以是ViewGroup、View或element,但必须只有一个根元素,并且必须包含xmlns:android属性和android命名空间,如图所示。的可能重复项
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 <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"
        >