我按照这里的教程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;
}
}
首先,我选择 TabArtist
它显示视图,但是当我切换到另一个选项卡时,然后再次切换回Artist
Tab,它会导致强制关闭。
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>
日志猫
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