Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error inflating Fragment in Dialog the second time

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?

like image 540
Heiko Rupp Avatar asked Aug 10 '11 08:08

Heiko Rupp


People also ask

How to show a Fragment as a dialog?

Showing the DialogFragment It is not necessary to manually create a FragmentTransaction to display your DialogFragment . Instead, use the show() method to display your dialog. You can pass a reference to a FragmentManager and a String to use as a FragmentTransaction tag.

How to show Fragment as dialog in android?

Overriding onCreateView() will let you show a Fragment as dialog and you can make the Title text customized. On the other hand, overriding onCreateDialog(), you can again show a fragment as dialog and here you can customize the entire dialog fragment. Means, you can inflate any view to show as dialog.

How to add fragments in activity?

Add a fragment to an activity You can add your fragment to the activity's view hierarchy either by defining the fragment in your activity's layout file or by defining a fragment container in your activity's layout file and then programmatically adding the fragment from within your activity.

How to define Fragment in android?

A fragment defines and manages its own layout, has its own lifecycle, and can handle its own input events. Fragments cannot live on their own--they must be hosted by an activity or another fragment. The fragment's view hierarchy becomes part of, or attaches to, the host's view hierarchy.


2 Answers

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

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.

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.

like image 124
NPike Avatar answered Sep 28 '22 11:09

NPike


I had a similar problem

I solved it very easy

deleting all fragments when close dialog

This is my dialog

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="310dp"
    android:layout_height="260dp" >

     <fragment
        android:layout_width="310dp"
        android:layout_height="260dp"
        android:tag="one"
        class="com.android.mushrooms.CookingProgressFragment" />

    <fragment
        android:layout_width="310dp"
        android:layout_height="260dp"
        android:tag = "two"
        class="com.android.mushrooms.CookingInfoFragment" />

</RelativeLayout>

and that's what happens when you delete a dialog

dialog.findViewById(R.id.cifButtonClose)
                .setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        mDragController.endDrag();

                        FragmentTransaction ft2 = getSupportFragmentManager()
                                .beginTransaction();

                        ft2.remove( getSupportFragmentManager()
                                .findFragmentByTag("one"));
                        ft2.remove( getSupportFragmentManager()
                                .findFragmentByTag("two"));
                        ft2.commit();
                        dialog.dismiss();
                    }
                });
like image 23
Sergei S Avatar answered Sep 28 '22 12:09

Sergei S