gpt4 book ai didi

android - 如何删除对 child parent 的看法?安卓

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:17:40 26 4
gpt4 key购买 nike

所以我有一个按钮,当它被点击时会显示一个警告对话框。我在 Activity 的 onCreate 方法中为警报对话框创建了 View 。代码就在这里:

    LayoutInflater factory = LayoutInflater.from(this);
view = factory.inflate(R.layout.grade_result, null);

当我第一次按下按钮时,对话框以我想要的方式显示,但是当我第二次按下它时它抛出这个异常。

11-28 00:35:58.066: E/AndroidRuntime(30348): Caused by: java.lang.IllegalStateException: 指定的子项已有父项。您必须先对 child 的 parent 调用 removeView()。

按下按钮时显示 AlertDialog 的方法代码就在这里:

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

任何帮助将不胜感激!谢谢!

最佳答案

膨胀应该在 AlertDialog 的生成器中设置的 View 对我有用:

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

关于android - 如何删除对 child parent 的看法?安卓,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13601005/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com