2017-05-08 1 views
0

Ich rufe eine Liste in einem Dialogfragment auf, das von einem benutzerdefinierten Adapter geladen wird.Dialogfragment java.lang.IllegalStateException: Das angegebene Kind hat bereits einen Elternteil. Sie müssen zuerst removeView() für das übergeordnete Element des Kindes aufrufen.

Laden mein Fragment

ProfileListViewHolder.imageButtonMore.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 

      ProfileDialog ProfileDialogFragment = new ProfileDialog(); 

      ProfileDialogFragment.setStyle(DialogFragment.STYLE_NORMAL, R.style.CustomDialog); 
      android.support.v4.app.FragmentTransaction fragmentManager =((AppCompatActivity)context).getSupportFragmentManager().beginTransaction(); 

      fragmentManager.replace(R.id.drawer_layout,ProfileDialogFragment) .addToBackStack(null).commit(); 

     } 
    }); 

und das ist mein Dialog, die ich verwende

public class ProfileDialog extends android.support.v4.app.DialogFragment { 

private Context context; 

public View profileView; 


@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
} 


public void setTitle(String title) { 
    Dialog dialog = getDialog(); 
    dialog.setTitle(title); 
} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 

    if (profileView == null) { 
      profileView = inflater.inflate(R.layout.fragment_profile_dialog, container,false); 
    } else { 
     ((ViewGroup) profileView .getParent()).removeView(profileView); 
    } 

    ListView profileListView = (ListView) profileView.findViewById(R.id.lstvSelectedProfile); 


    return profileListView; 
} 

@Override 
public void onStart() { 
    super.onStart(); 
    //addInnerFragment(); 
} 

@Override 
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { 
    super.onViewCreated(view, savedInstanceState); 


} 
@Override 
public Dialog onCreateDialog(Bundle savedInstanceState) { 
    Dialog dialog = super.onCreateDialog(savedInstanceState); 
    return dialog; 
} 

} 
+1

Was ist der Zweck der '((Viewgroup) profileView.getParent()). RemoveView (profileView)'? – azizbekian

+0

@azizbekian: Ich nahm mich das kann helfen, die Eltern zu entfernen, die hier beunruhigend sein kann –

+0

Wenn Sie verwenden möchten, 'ProfileDialog' als Dialog, müssen Sie es verwenden' show() 'Methode, statt die Transaktion du bist dort benutzen. –

Antwort

0

Sie sind die profileListView statt profileView zurück. Ändern Sie den Code wie folgt,

@Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     profileView = inflater.inflate(R.layout.fragment_profile_dialog, container, false); 
     ListView profileListView = (ListView) profileView.findViewById(R.id.lstvSelectedProfile); 
     return profileView; 
    } 
0

Versuchen

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 

    profileView = inflater.inflate(R.layout.fragment_profile_dialog, container,false); 

    ListView profileListView = (ListView) profileView.findViewById(R.id.lstvSelectedProfile); 

    return profileView; 
} 
+0

Wie ist das anders als Krishs Antwort? –

Verwandte Themen