我是新的Android和我嘗試使Android蜂窩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 .............標記爲null或父級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>
感謝您的幫助。
您是否嘗試按錯誤消息的建議爲每個片段分配唯一標記? – Dave
另外我想還有第二個佈局XML文件,因爲您包含的文件沒有名爲'fragments'的視圖。 – Dave
嗨,我試過每個片段上的唯一標籤,但錯誤仍然存在。我檢查了我的XML文件,並沒有其他視圖被命名爲「fragments」...感謝您的回答 – Florian