错误对话框中的片段充气第二次片段、错误、对话框中

2023-09-12 07:44:12 作者:我的爱太卑微

我有以下的code在于,对于包含片段的布局开始一个对话的活动。

I have the following code in an Activity that starts a dialog for a layout that contains a fragment.

...
case R.id.pick_resource:
        dialog = new Dialog(this);
        dialog.setContentView(R.layout.resource_picker);
        dialog.setCancelable(true);
        dialog.setTitle("Pick a resource");
        dialog.show();

这工作得很好后的第一时间应用程序启动,但是当退出对话框,后来又打电话,我得到这个堆栈跟踪:

This works very well the first time after application start, but when the dialog is quit and later called again, I get this stack trace:

08-10 10:47:33.990: ERROR/AndroidRuntime(26521): FATAL EXCEPTION: main
        android.view.InflateException: Binary XML file line #7: Error inflating class fragment
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:688)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:724)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:479)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:391)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:347)
        at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:224)
        at android.app.Dialog.setContentView(Dialog.java:449)
        at org.rhq.pocket.StartActivity.onOptionsItemSelected(StartActivity.java:118)
        at android.app.Activity.onMenuItemSelected(Activity.java:2390)
        at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:852)
        at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:153)
        at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:956)
        at com.android.internal.view.menu.ActionMenuView.invokeItem(ActionMenuView.java:174)
        at com.android.internal.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:85)
        at android.view.View.performClick(View.java:3100)
        at android.view.View$PerformClick.run(View.java:11644)
        at android.os.Handler.handleCallback(Handler.java:587)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:126)
        at android.app.ActivityThread.main(ActivityThread.java:3997)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:491)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
        at dalvik.system.NativeStart.main(Native Method)
        Caused by: java.lang.IllegalArgumentException: Binary XML file line #7: Duplicate id 0x7f090007, tag null, or parent id 0xffffffff with another fragment for org.rhq.pocket.ResourcePickerFragement
        at android.app.Activity.onCreateView(Activity.java:4089)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:664)
        ... 24 more

什么想法,有什么可能会导致此异常?我必须以某种方式卸载片段?

Any idea, what may cause this exception? Do I have to unload the fragment somehow?

推荐答案

请问在布局片段有一个Android:id属性?

Does the fragment in your layout have an android:id attribute?

我怀疑这是因为该片段布局被充气每次实例化,在第一次的ID不被使用,但在第二次FragmentManager仍然认为你的片段是活的,所以将ID视为重复

I suspect this is because the fragment is instantiated each time your layout is inflated, the first time the ID isn't being used, but the second time the FragmentManager still thinks your Fragment is alive, so the ID is considered a duplicate.

尝试取出了android:id属性从你的片段,如果它存在,或者添加占位符布局如的FrameLayout和使用fragmenttransaction动态创建的对话框每次添加片段

Try removing the android:id attribute from your fragment if it exists, or add a placeholder layout such as a framelayout and use a fragmenttransaction to dynamically add the fragment each time your dialog is created.