4

我有一个片段SearchPageFragment(代表平板电脑上的主视图),它本身包含两个片段:

<fragment   android:name="com.test.fragments.SearchFormFragment"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="2dp"
            android:id="@+id/searchFormFragment"
            android:tag="searchFormFragmentTag">
    <!-- Preview: layout=@layout/fragment_search_form -->
</fragment>           
<fragment   android:name="com.test.fragments.SearchResultsFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/searchResultsFragment">
    <!-- Preview: layout=@layout/fragment_search_results -->
</fragment>

一切正常 - 页面加载,一切正常......直到你改变方向。当您执行以下错误时:

java.lang.IllegalArgumentException: Binary XML file line #10: Duplicate id 0x7f08001f, tag searchFormFragmentTag, or parent id 0x0 with another fragment for com.test.fragments.SearchFormFragment
                                    at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:262)
                                    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:563)

以下是切换方向时的调用顺序:

SearchPageFragment(4911): Creating...
SearchPageFragment(4911): Inflating...
    SearchFormFragment(4911): Creating...
    SearchFormFragment(4911): Inflating...
    SearchResultsFragment(4911): Creating...
    SearchResultsFragment(4911): Inflating...

// Change Orientation

SearchPageFragment(4911): Pausing...
    SearchFormFragment(4911): Pausing...
    SearchResultsFragment(4911): Pausing...
SearchPageFragment(4911): Destroying...
    SearchFormFragment(4911): Destroying...
    SearchResultsFragment(4911): Destroying...

// All Seems Normal - But...

SearchPageFragment(4911): Creating...
SearchPageFragment(4911): Creating...
SearchPageFragment(4911): Inflating...
    SearchFormFragment(4911): Creating...
    SearchFormFragment(4911): Inflating...
    SearchResultsFragment(4911): Creating...
    SearchResultsFragment(4911): Inflating...
SearchPageFragment(4911): Inflating...

// SearchPageFragment has started twice and crashes when the second one inflates a ***unique*** component

AndroidRuntime(4911): Shutting down VM

有人知道为什么我的容器片段在方向改变时会被调用两次吗?

4

2 回答 2

5

知道了:

该错误是 SearchPageFragment 的持有人活动添加了Fragment两次。

当方向发生变化时,Android 将始终保留附加到视图的片段。

因此,您需要确保如果您在onCreate()方法中添加 Fragment,则使用 if 语句围绕它的创建(以及添加/替换转换)来检查它savedInstanceState是否为空(如果它不为空,则表明方向更改已发生了)。

if(savedInstanceState == null) {
    // Add fragment code here
}
于 2011-07-20T10:55:53.247 回答
1

我用这个解决了这个问题。

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
         if (root != null) {
                ViewGroup parent = (ViewGroup) root.getParent();
                if (parent != null)
                    parent.removeView(root);
            }
            try {
                root = inflater.inflate(R.layout.activity_explore,container,false);
            } catch (InflateException e) {
                /* map is already there, just return view as it is */
            }
 return root;
}
于 2013-07-02T09:37:21.337 回答