0

我有一个使用带有选项卡的 ActionBar 的 SherlockFragmentActivity。其中一个选项卡是一个 SherlockFragment,它有一个包含片段的布局文件。首次显示该选项卡时,一切都很好。如果我切换到另一个选项卡,然后返回到该选项卡,我会崩溃:

02-21 10:25:10.077: E/AndroidRuntime(3916): Caused by: java.lang.IllegalArgumentException: Binary XML file line #29: Duplicate id 0x7f06006e, tag null, or parent id 0x0 with another fragment for com.nexapps.myq.fragments.TitleInfo 02-21 10:25:10.077: E/AndroidRuntime(3916): at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:275) 02-21 10:25:10.077: E/AndroidRuntime(3916): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:676) 02-21 10:25:10.077: E/AndroidRuntime(3916): ... 21 more

我的主要活动:

public class TitleActivity extends SherlockFragmentActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {       
        ...
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        ...
        Tab tab = actionBar.newTab().setText(R.string.details);
        tab.setTabListener(new TabListener<TitleDetailsFragment>(this, "details", TitleDetailsFragment.class));
        actionBar.addTab(tab);         
    }

}

标签监听器:公共类 TabListener 实现 com.actionbarsherlock.app.ActionBar.TabListener { private Fragment mFragment; 私人最终活动 mActivity;私有最终字符串 mTag;私有最终类 mClass;

  public TabListener(Activity activity, String tag, Class<T> cls) {
      mActivity = activity;
      mTag = tag;
      mClass = cls;
  }

  public TabListener(Activity activity, String tag, Class<T> clz, Fragment fragment) {
      mActivity = activity;
      mTag = tag;
      mClass = clz;
      mFragment = fragment;
  }

  public void onTabSelected(Tab tab, FragmentTransaction ft) {
      if (mFragment == null) {
          mFragment = Fragment.instantiate(mActivity, mClass.getName());
          ft.add(android.R.id.content, mFragment, mTag);
      } else {
          ft.attach(mFragment);
      }

      TabChangeInterface tci = (TabChangeInterface) mActivity;
      if (tci != null) {
        tci.tabSelect(mTag);
      }
  }

  public void onTabUnselected(Tab tab, FragmentTransaction ft) {
      if (mFragment != null) {
          ft.detach(mFragment);
      }
  }

  public void onTabReselected(Tab tab, FragmentTransaction ft) {
  }

}

我的标签片段:

public class TitleDetailsFragment extends SherlockFragment {
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.activity_title, container, false);
        return view;
    }
}

R.layout.activity_title:

<fragment
   android:id="@+id/info"
   android:name="foo.TitleInfo"
   ...

我可以删除 id (这解决了问题),但这些有助于状态更改。关于如何保留 ID 并解决问题的任何想法?

4

2 回答 2

0

您不能删除嵌入在 xml 布局中的片段。为此,您应该更改您的 xml 以包含 a <FrameLayout android:id="fragment_container" .../>,然后通过调用以编程方式替换框架中的片段:

MyFrag newFragment = new MyFrag();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.fragment_container,newFragment,"myFragName")
.addToBackStack("myFragName")//optional
.commit();

假设您正在使用支持包,否则您只需更改getSupportFragmentManager()getFragmentManager()

您可以在此处阅读有关使用片段的更多信息:http: //developer.android.com/training/basics/fragments/index.html

编辑:更新以包含基于新信息的代码

R.layout.activity_title:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Title" >

    <FrameLayout
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>

TitleActivity.class:

public class TitleActivity extends SherlockFragmentActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {       
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_title);        
         ...
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        ...
        Tab tab = actionBar.newTab().setText(R.string.details);
        tab.setTabListener(new TabListener<TitleDetailsFragment>(this, "details", TitleDetailsFragment.class));
        actionBar.addTab(tab);         
    }

}

R.layout.details_fragment:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/textView1"
       android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical|center_horizontal"
        android:textSize="10sp"
        android:textStyle="italic"
        android:text="Details are here!"/>
</RelativeLayout>

标签片段:

public class TitleDetailsFragment extends SherlockFragment {

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
       return inflater.inflate(R.layout.details_fragment, container, false);
    }
}

您可以保持与上面的代码相同的 TabListener。

于 2013-02-21T19:42:54.843 回答
-1

所以这与选项卡内容是一个片段的事实有关,而片段不能包含片段。因此,请在此处改用视图或使用事务手动删除/添加。

于 2013-02-22T00:27:58.733 回答