我有一个自定义对话框。当我单击“确定”时,我希望它从其 EditText 字段中获取文本。但是,它会引发空指针异常。
代码:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.dialog_delivered, (ViewGroup)findViewById(R.id.llDeliveredDialog));
builder.setMessage("Dialog message");
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
final EditText text = (EditText)findViewById(R.id.etGivenTo);
String value = text.getText().toString();
}
});
builder.setCancelable(false);
builder.setView(layout);
builder.show();
XML 布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/llDeliveredDialog"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<EditText
android:id="@+id/etGivenTo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ems="10" >
<requestFocus />
</EditText>
</LinearLayout>
任何帮助将非常感激。