把每天出现的新崩溃记录下来 。
要做一个对话框,弹出界面有个文本输入框,我把mEditText定义全局变量,第一次弹出没有问题,点击再次弹出崩溃。代码如下:
private void openDialog() { AlertDialog.Builder builder = new AlertDialog.Builder(this); mEditText.setText(""); builder.setTitle("请输入").setView(mEditText).setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { if (TextUtils.isEmpty(mEditText.getText().toString())) { Toast.makeText(HomeActivity.this, "任务名称不能为空!", Toast.LENGTH_SHORT).show(); return; } save(); } }).setNegativeButton("取消", null).create().show(); }
异常信息如下:
java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
其实异常信息很明显了。
对症下药:
private void openDialog() { AlertDialog.Builder builder = new AlertDialog.Builder(this); mEditText.setText(""); ViewGroup viewGroup = (ViewGroup) mEditText.getParent(); if (viewGroup != null) viewGroup.removeAllViewsInLayout(); builder.setTitle("请输入").setView(mEditText).setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { if (TextUtils.isEmpty(mEditText.getText().toString())) { Toast.makeText(HomeActivity.this, "任务名称不能为空!", Toast.LENGTH_SHORT).show(); return; } save(); } }).setNegativeButton("取消", null).create().show(); }