问题
I'm trying to get text from values of edit field showed on a dialog and save it in a variable.
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.dialg);
dialog.setTitle("Title...");
dialog.show();
Button dialogButtonCancel = (Button) dialog.findViewById(R.id.cancel);
// if button is clicked, close the custom dialog
dialogButtonCancel.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();}});
Button dialogButtonOK = (Button) dialog.findViewById(R.id.OK);
//***************************************************************************
dialogButtonOK.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
EditText edt1=(EditText)findViewById(R.id.EditTextNom);
nom = edt1.getText().toString();
EditText edt2=(EditText)findViewById(R.id.editTextDescription);
description = edt2.getText().toString();
dialog.dismiss();
}});
回答1:
Try instead
EditText edt1=(EditText)dialog.findViewById(R.id.EditTextNom);
you need to look in the layout that is inflated for the Dialog
. Right now it is looking in the one that was inflated for the Activity
and, obviously, those View
s don't exist in that layout
.
来源:https://stackoverflow.com/questions/22724710/getting-values-of-edit-text-error-nullpointer-why