我是Android上的新手,我尝试在Android Honeycomb 3.0上制作应用程序 这是我的问题:我的操作栏中有2个标签。选项卡1使用片段A和B,选项卡2使用片段C和D.当我加载应用程序时,选择选项卡1并显示片段A和B.然后我点击标签2,它也可以正常工作。但当我返回选项卡1时,应用程序崩溃并显示以下错误:
android.view.InflateException:二进制XML文件行#6:错误膨胀类片段..... ..... 引起:java.lang.IllegalArgumentException:二进制XML文件行#6:重复 id 0x7f ............. tag null或parent id 0x .......
这是我的代码:
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ActionBar bar = getActionBar();
bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
bar.setDisplayShowTitleEnabled(false);
ActionBar.Tab tab1 = bar.newTab().setText("tab 1");
ActionBar.Tab tab2 = bar.newTab().setText("tab 2");
Fragment frag1 = new FragmentOne();
Fragment frag2 = new FragmentTwo();
tab1.setTabListener(new MyTabListener(frag1));
tab2.setTabListener(new MyTabListener(frag2));
bar.addTab(tab1);
bar.addTab(tab2);
}
private class MyTabListener implements ActionBar.TabListener {
private Fragment mFragment;
// Called to create an instance of the listener when adding a new tab
public MyTabListener(Fragment fragment) {
mFragment = fragment;
}
public void onTabSelected(Tab tab, FragmentTransaction ft) {
ft.add(R.id.fragments, mFragment);
}
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
ft.remove(mFragment);
}
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// do nothing
}
}
片段1:
public class FragmentOne extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View mainView = inflater.inflate(R.layout.fragments, container, false);
return mainView;
}
}
fragments.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
xmlns:android="http://schemas.android.com/apk/res/android"
android:name="ch.comem.test.FragmentOneA"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/idFragment_one_a"
android:layout_weight="30">
</fragment>
<fragment
xmlns:android="http://schemas.android.com/apk/res/android"
android:name="ch.comem.test.FragmentOneB"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/idFragment_one_b"
android:layout_weight="70">
</fragment>
感谢您的帮助。
答案 0 :(得分:1)
我看到的主要问题是,您的FragmentOne
课程夸大fragments.xml
,其中包含对另外两个片段FragmentOne
和FragmentTwo
的引用。这是无效的,因为碎片不能包含其他碎片。