意外的命名空间preFIX"的xmlns"发现标签片段 [英] Unexpected namespace prefix "xmlns" found for tag fragment

查看:298
本文介绍了意外的命名空间preFIX"的xmlns"发现标签片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到一个错误:

意外的命名空间preFIX的xmlns找到的标记片段

Unexpected namespace prefix "xmlns" found for tag fragment

有关行

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

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

推荐答案

删除此

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

在XML片段

http://developer.android.com/guide/topics/资源/布局resource.html

根元素可以是一个ViewGroup中,视图或元素,但只能有一个根元素,它必须包含的xmlns:android的属性与Android命名空间,如图

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

<一个href=\"http://stackoverflow.com/questions/16511605/unexpected-namespace-$p$pfix-xmlns-for-tag-fragment\">Unexpected命名空间preFIX&QUOT;的xmlns&QUOT;对于标记片段

<一个href=\"http://stackoverflow.com/questions/14916638/unexpected-namespace-$p$pfix-xmlns-found-for-tag-linearlayout\">Unexpected命名空间preFIX&QUOT;的xmlns&QUOT;发现标签的LinearLayout

<一个href=\"http://stackoverflow.com/questions/16952259/unexpected-namespace-$p$pfix-xmlns-found-for-tag-listview\">Unexpected命名空间preFIX&QUOT;的xmlns&QUOT;发现标签的ListView

编辑:

从您在评论张贴链接,它看起来像你正在寻找的是卡口与片段。因此,尝试下面并修改根据您的要求。

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.

例如:

我已经设置分SDK是11

I have set min sdk is 11

MainActivity.java

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

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

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>

C片段XML

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>

快照

这篇关于意外的命名空间preFIX&QUOT;的xmlns&QUOT;发现标签片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆