0

我是新的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> 

感谢您的帮助。

+0

您是否尝试按错误消息的建议为每个片段分配唯一标记? – Dave

+0

另外我想还有第二个布局XML文件,因为您包含的文件没有名为'fragments'的视图。 – Dave

+0

嗨,我试过每个片段上的唯一标签,但错误仍然存​​在。我检查了我的XML文件,并没有其他视图被命名为“fragments”...感谢您的回答 – Florian

回答

1

我看到的主要问题是您的FragmentOne类充气fragments.xml,其本身包含对两个其他片段FragmentOneFragmentTwo的引用。这是无效的,因为碎片不能包含其他碎片。

+0

谢谢,它现在有效。我在MainActivity中调用'fragments.xml',它工作正常。但我仍然有一个问题:现在在我的两个选项卡中,每次都有两个片段。如果我想要在第一个标签1片段和第二个片段中,我该怎么办?谢谢你的帮助! – Florian

相关问题