【问题标题】:You must call removeView() on the child's parent first with AlertView您必须先使用 AlertView 在孩子的父母上调用 removeView()
【发布时间】:2015-06-22 01:11:17
【问题描述】:

我有一个警告对话框,我将使用 TextView 获取文本,但是当我第二次调用它时,应用程序崩溃并出现错误:

04-15 19:37:48.433: E/AndroidRuntime(907): java.lang.IllegalStateException: 
    The specified child already has a parent. You must call removeView() on 
    the child's parent first.

我的 Java 源代码:

public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final RelativeLayout rLayout = (RelativeLayout) findViewById(R.id.relativeLayout);
        Button btn1 = (Button) findViewById(R.id.button1);
        final AlertDialog.Builder build = new AlertDialog.Builder(MainActivity.this);
        build.setTitle("Ders Adı Giriniz");
        final EditText dersAdiGir = new  EditText(MainActivity.this);
        build.setView(dersAdiGir);
        final LinearLayout layoutDers = (LinearLayout) findViewById(R.id.layoutDers);

        build.setPositiveButton("Tamam", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {

                Editable girilenDers = dersAdiGir.getText();
                TextView tv1 = new TextView(MainActivity.this);
                tv1.setText(girilenDers);
                layoutDers.addView(tv1);
                dialog.dismiss();
                build.create();

            }
        });

        btn1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                AlertDialog alert = build.create();
                alert.show();
            }
        });




    }
}

请帮帮我,谢谢大家

【问题讨论】:

    标签: java android android-edittext android-alertdialog


    【解决方案1】:

    每次单击按钮时,您都会创建一个 AlertDialog 的新实例。在 OnClickListener 内部类之外创建最终的 AlertDialog

    解决方法如下:

    final AlertDialog alert = build.create();
    btn1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            alert.show();
        }
    });
    

    【讨论】:

    • 哇哦哦!谢谢修复:)
    • 在带有警报 final AlertDialog.Builder 的 ViewHolder 中不起作用
    猜你喜欢
    • 1970-01-01
    • 2018-06-05
    • 2013-10-18
    • 2015-09-06
    • 2020-02-16
    • 2014-06-02
    • 1970-01-01
    相关资源
    最近更新 更多