如何删除视图对孩子的父母?机器人 [英] How to remove view on the child's parent? android
问题描述
所以,我有一个按钮,当点击它显示一个警告对话框。我创建视图在我活动的onCreate方法警告对话框。在code因为这是在这里:
So i have a button that displays an alert dialog when its clicked. I create the view for the alert dialog in the onCreate method of my activity. The code for that is right here:
LayoutInflater factory = LayoutInflater.from(this);
view = factory.inflate(R.layout.grade_result, null);
当我按下按钮,第一次,对话框显示我想要的方式,但是当我推第二次,它抛出此异常
When i push the button for the first time, the dialog displays the way i want it, but when i push it a second time it throws this exception
11-28 00:35:58.066:E / AndroidRuntime(30348):java.lang.IllegalStateException:产生的原因指定的孩子已经有一个父。你必须先调用removeView()孩子的父母。
11-28 00:35:58.066: E/AndroidRuntime(30348): Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
我的$ C $下显示的AlertDialog当按下按钮的方法就在这里:
My code for the method that displays the AlertDialog when the button is pushed is right here:
public void details(View v){
final AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setView(view);
alert.setMessage("Details About Your Grades")
.setCancelable(false)
.setPositiveButton("Continue", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id){
dialog.cancel();
}
});
alert.show();
任何帮助将是AP preciated!谢谢!
Any help would be appreciated! Thank you!
推荐答案
充气工作对我来说,应该在AlertDialog的生成器内设置的观点:
Inflating the view that should be set within the Builder of the AlertDialog worked for me :
Button mButton = (Button) findViewById(R.id.my_button);
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// inflating view out of the onClick() method does not work
LayoutInflater mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final ViewGroup viewGroup= (ViewGroup) mInflater.inflate(R.layout.my_view, null);
Dialog alertDialog = new AlertDialog.Builder(getActivity())
.setTitle(R.string.my_title)
.setView(viewGroup)
.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
}).create();
alertDialog.show();
}
}
这篇关于如何删除视图对孩子的父母?机器人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!