Error inflating Fragment in Dialog the second time

前端 未结 6 877
深忆病人
深忆病人 2020-12-03 08:31

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 Dialo         


        
相关标签:
6条回答
  • Try as follows

    public class ABDCD{
        private Dialog dialog = null;
        private View viewHoldingDialog = null;
        ----------------------------
    
    case R.id.pick_resource:
        dialog = new Dialog(this,R.style.Theme_Dialog_Translucent);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        viewHoldingDialog = (ViewGroup) layoutInflater.inflate(
                R.layout.resource_picker, null);
        dialogLayoutParams = new LayoutParams(android.view.WindowManager.LayoutParams.WRAP_CONTENT, android.view.WindowManager.LayoutParams.WRAP_CONTENT);
        dialog.addContentView(viewHoldingDialog,
    
    
        dialogLayoutParams);
    

    }

    0 讨论(0)
  • 2020-12-03 08:57

    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.

    0 讨论(0)
  • 2020-12-03 09:00

    Maybe this can help someone:

    I use the code of Эвансгелист Evansgelist, but with a DialogFragment that has a layout with a MapFragment, the code have to be onDestroy block:

    @Override
    public void onDestroy(){
        super.onDestroy();
        FragmentTransaction ft2 = getActivity().getFragmentManager()
                .beginTransaction();
    
        ft2.remove( getFragmentManager()
                .findFragmentById(R.id.map_map));
        ft2.commit();
    }
    

    This is my layout:

    <?xml version="1.0" encoding="utf-8"?>
    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
             xmlns:tools="http://schemas.android.com/tools"
             android:layout_width="match_parent"
             android:layout_height="match_parent"
             tools:context="com.example"
    >
    <fragment
          android:id="@+id/map_map"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:name="com.google.android.gms.maps.MapFragment"/>
    </FrameLayout>
    
    0 讨论(0)
  • 2020-12-03 09:02

    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();
                        }
                    });
    
    0 讨论(0)
  • 2020-12-03 09:03

    Another answer that could help someone else: I had a similar problem but instead of destroying the fragment in the onDestroy, I had to move it to onPause in order to get it to work:

    @Override
    protected void onPause() {
        if(adFragment != null){
            childFragmentManager.beginTransaction().remove(adFragment).commitAllowingStateLoss();
        }
        super.onPause();
    }
    
    0 讨论(0)
  • 2020-12-03 09:04
    On android.view.inflateexception: binary xml file line #8: error inflating class fragment error :
    

    Try removing the android:id attribute from your fragment if it exists. and on dismiss dialog add the following code: it works for me

    activity.getFragmentManager().beginTransaction().remove(activity.getFragmentManager().findFragmentById(R.id.mMap_location)).commit();
    
    0 讨论(0)
提交回复
热议问题