2017-05-08 71 views
0

我正在調用從自定義適配器加載的對話框片段中的列表。Dialog Fragment java.lang.IllegalStateException:指定的子項已經有父項。您必須先調用子對象的父對象的removeView()。

加載我的片段

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(); 

     } 
    }); 

,這是我的對話,我使用

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

什麼目的'((ViewGroup中)profileView.getParent())。removeView(profileView)'? – azizbekian

+0

@azizbekian:我認爲這可能會幫助我刪除可能在這裏困擾的父項 –

+0

如果您想使用'ProfileDialog'作爲對話框,則需要使用它的show()方法,而不是使用事務在那裏使用。 –

回答

0

要退回profileListView而不是profileView。改變這樣的代碼,

@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

嘗試

@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

這與Krish的回答有什麼不同? –

相關問題