我正在尝试从嵌套的alertdialog获取文本输入。为什么字符串Res1始终为null?从下面的Logcat中,我认为我需要一个ViewGroup,但是如何/在哪里更改代码,以使Alertdialogs完全完成后能够读取两个EditText?这两个EditText都在custom.dialog.xml中定义。
也许有更好的方法可以做到这一点-我本质上是在尝试使用Alertdialog来获取用户对2个后备问题的输入。我认为Android Nested dialog - Specified child already has a parent接近吗?
AlertDialog.Builder ad1 = new AlertDialog.Builder(this);
LayoutInflater inflater = this.getLayoutInflater();
View dv1 = inflater.inflate(R.layout.custom_dialog, null);
ad1.setView(dv1);
EditText edTitle = (EditText) dv1.findViewById(R.id.edTitle);
ad1.setTitle("New Title");
ad1.setMessage("Enter text below");
ad1.setPositiveButton("Add", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
AlertDialog.Builder ad2 = new AlertDialog.Builder(MainActivity.this);
LayoutInflater inflater2 = MainActivity.this.getLayoutInflater();
View dv2 = inflater2.inflate(R.layout.custom_dialog, null);
ad2.setView(dv2);
EditText edText = (EditText) dv2.findViewById(R.id.edText);
ad2.setTitle("New Text:");
ad2.setMessage("Enter text below")
ad2.setPositiveButton("Done", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
String Res1 = edText.getText().toString();
String Res2 = edTitle.getText().toString();
}
});
ad2.setNegativeButton(sCancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
dialog.dismiss();
AlertDialog adText = ad2.create();
adText.show();
}
});
ad1.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
dialog.dismiss();
}
});
AlertDialog adTitle = ad1.create();
adTitle.show();
和Logcat:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.dhj.tt_V2, PID: 20303
java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
at com.example.dhj.tt_V2.MainActivity$2.onClick(MainActivity.java:374)