片段替换和后退

时间:2014-03-26 21:27:07

标签: android android-fragments

我有一个活动有一个片段,当我点击一个按钮时,我这样做

              Fragment newFragment = new OpinionesFragment();
              FragmentTransaction transaction = getFragmentManager().beginTransaction();

              // Replace whatever is in the fragment_container view with this fragment,
              // and add the transaction to the back stack
              transaction.replace(R.id.content_frame, newFragment);
              transaction.addToBackStack(null);

              // Commit the transaction
              transaction.commit();

屏幕上会出现一个新片段。后来我按下手机的按钮,当我点击另一个按钮时,应用程序给我一个错误。怎么了?避免这种情况的代码是什么?谢谢,这是我的原木猫

03-26 22:38:05.760: E/AndroidRuntime(4426): FATAL EXCEPTION: main
03-26 22:38:05.760: E/AndroidRuntime(4426): android.view.InflateException: Binary XML file line #116: Error inflating class fragment
03-26 22:38:05.760: E/AndroidRuntime(4426):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:587)
03-26 22:38:05.760: E/AndroidRuntime(4426):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
03-26 22:38:05.760: E/AndroidRuntime(4426):     at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
03-26 22:38:05.760: E/AndroidRuntime(4426):     at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
03-26 22:38:05.760: E/AndroidRuntime(4426):     at com.triggertrap.sample.DetallesFragment.onCreateView(DetallesFragment.java:70)
03-26 22:38:05.760: E/AndroidRuntime(4426):     at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
03-26 22:38:05.760: E/AndroidRuntime(4426):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
03-26 22:38:05.760: E/AndroidRuntime(4426):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
03-26 22:38:05.760: E/AndroidRuntime(4426):     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
03-26 22:38:05.760: E/AndroidRuntime(4426):     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
03-26 22:38:05.760: E/AndroidRuntime(4426):     at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:440)
03-26 22:38:05.760: E/AndroidRuntime(4426):     at android.os.Handler.handleCallback(Handler.java:587)
03-26 22:38:05.760: E/AndroidRuntime(4426):     at android.os.Handler.dispatchMessage(Handler.java:92)
03-26 22:38:05.760: E/AndroidRuntime(4426):     at android.os.Looper.loop(Looper.java:130)
03-26 22:38:05.760: E/AndroidRuntime(4426):     at android.app.ActivityThread.main(ActivityThread.java:3835)
03-26 22:38:05.760: E/AndroidRuntime(4426):     at java.lang.reflect.Method.invokeNative(Native Method)
03-26 22:38:05.760: E/AndroidRuntime(4426):     at java.lang.reflect.Method.invoke(Method.java:507)
03-26 22:38:05.760: E/AndroidRuntime(4426):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:847)
03-26 22:38:05.760: E/AndroidRuntime(4426):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
03-26 22:38:05.760: E/AndroidRuntime(4426):     at dalvik.system.NativeStart.main(Native Method)
03-26 22:38:05.760: E/AndroidRuntime(4426): Caused by: java.lang.IllegalArgumentException: Binary XML file line #116: Duplicate id 0x7f080053, tag null, or parent id 0x0 with another fragment for com.triggertrap.sample.OpinionesFragment
03-26 22:38:05.760: E/AndroidRuntime(4426):     at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:296)
03-26 22:38:05.760: E/AndroidRuntime(4426):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:563)
03-26 22:38:05.760: E/AndroidRuntime(4426):     ... 19 more

1 个答案:

答案 0 :(得分:1)

尝试覆盖后退按钮的行为。

@Override
public void onBackPressed() {

    int count = getFragmentManager().getBackStackEntryCount();

    if (count == 0) {
        // do nothing given that you might get the same error.
    } else {
        getFragmentManager().popBackStack(); // display previous fragment in the stack
    }

}

希望它有所帮助!