在Fragment中重新创建View时强制关闭(重复片段)

时间:2012-05-27 05:10:14

标签: android android-fragments android-actionbar

我在这里按照教程Android Action Bar

并在ArtistFragment.class

public class ArtistFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View fragView = inflater.inflate(R.layout.main_fragment, container, false);
        return fragView;
    }
}

首先,我选择显示视图的标签Artist但是,当我切换到另一个标签时,再次切换回Artist标签,它会导致强制关闭。

main_fragment.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"
    android:orientation="horizontal"
    android:padding="10dp"
    android:id="@+id/frags" >

    <fragment 
        android:name="com.android.custom.FragmentLeft"
        android:id="@+id/fragment_left"
        android:layout_weight="0.25"
        android:layout_width="0dp"
        android:layout_height="match_parent"/>
    <fragment 
        android:name="com.android.custom.FragmentRight"
        android:id="@+id/fragment_right"
        android:layout_weight="0.75"
        android:layout_width="0dp"
        android:layout_height="match_parent"/>

</LinearLayout>

logcat的

05-27 11:58:36.398: E/AndroidRuntime(4216): FATAL EXCEPTION: main
05-27 11:58:36.398: E/AndroidRuntime(4216): android.view.InflateException: Binary XML file line #9: Error inflating class fragment
05-27 11:58:36.398: E/AndroidRuntime(4216):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:688)
05-27 11:58:36.398: E/AndroidRuntime(4216):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:724)
05-27 11:58:36.398: E/AndroidRuntime(4216):     at android.view.LayoutInflater.inflate(LayoutInflater.java:479)
05-27 11:58:36.398: E/AndroidRuntime(4216):     at android.view.LayoutInflater.inflate(LayoutInflater.java:391)
05-27 11:58:36.398: E/AndroidRuntime(4216):     at com.android.custom.ArtistFragment.onCreateView(ArtistFragment.java:20)
05-27 11:58:36.398: E/AndroidRuntime(4216):     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:776)
05-27 11:58:36.398: E/AndroidRuntime(4216):     at android.app.FragmentManagerImpl.attachFragment(FragmentManager.java:1133)
05-27 11:58:36.398: E/AndroidRuntime(4216):     at android.app.BackStackRecord.run(BackStackRecord.java:628)
05-27 11:58:36.398: E/AndroidRuntime(4216):     at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1309)
05-27 11:58:36.398: E/AndroidRuntime(4216):     at android.app.FragmentManagerImpl$1.run(FragmentManager.java:398)
05-27 11:58:36.398: E/AndroidRuntime(4216):     at android.os.Handler.handleCallback(Handler.java:587)
05-27 11:58:36.398: E/AndroidRuntime(4216):     at android.os.Handler.dispatchMessage(Handler.java:92)
05-27 11:58:36.398: E/AndroidRuntime(4216):     at android.os.Looper.loop(Looper.java:132)
05-27 11:58:36.398: E/AndroidRuntime(4216):     at android.app.ActivityThread.main(ActivityThread.java:4123)
05-27 11:58:36.398: E/AndroidRuntime(4216):     at java.lang.reflect.Method.invokeNative(Native Method)
05-27 11:58:36.398: E/AndroidRuntime(4216):     at java.lang.reflect.Method.invoke(Method.java:491)
05-27 11:58:36.398: E/AndroidRuntime(4216):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
05-27 11:58:36.398: E/AndroidRuntime(4216):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
05-27 11:58:36.398: E/AndroidRuntime(4216):     at dalvik.system.NativeStart.main(Native Method)
05-27 11:58:36.398: E/AndroidRuntime(4216): Caused by: java.lang.IllegalArgumentException: Binary XML file line #9: Duplicate id 0x7f080011, tag null, or parent id 0x7f080010 with another fragment for com.android.custom.FragmentLeft
05-27 11:58:36.398: E/AndroidRuntime(4216):     at android.app.Activity.onCreateView(Activity.java:4182)
05-27 11:58:36.398: E/AndroidRuntime(4216):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:664)
05-27 11:58:36.398: E/AndroidRuntime(4216):     ... 18 more

2 个答案:

答案 0 :(得分:0)

为什么R.layout.main_fragment里面有两个片段? Fragment内部不能包含其他Fragment ...它必须位于Activity内。是否有artist_fragment.xml你打算膨胀?

答案 1 :(得分:0)

将代码包含在<{p>中的onCreate方法中

if(savesInstanceState == null) {
    // your code here
}

这将阻止框架尝试重新创建方向或活动更改的所有内容(这是我认为正在发生的事情)。你应该(在大多数情况下)在每个活动中都有这个,所以框架可以像大多数人期望的那样运作。