I have an alert dialog with a xml layout for it and it has an edit text. When I write some text and press SAVE, the editText to String returns null. Here is my code -
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
dialogView = inflater.inflate(R.layout.dialog_layout, null);
alertDialogBuilder.setView(dialogView);
alertDialogBuilder.setView(R.layout.dialog_layout);
// set dialog message
alertDialogBuilder
.setCancelable(false)
.setPositiveButton("SAVE",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
EditText userInput = (EditText) dialogView.findViewById(R.id.editText);
dbUtils.insertCSVFileName(userInput.getText().toString());
Log.d("hi","vaqlue of edittext" + userInput.getText().toString()); //gives me null
}
})
My edit text is present in dialog_layout.xml
. Why is this null?
Your problem is that you set the layout twice:
alertDialogBuilder.setView(dialogView);
alertDialogBuilder.setView(R.layout.dialog_layout);
just do it with one of the methods above, remove one.
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
dialogView = inflater.inflate(R.layout.dialog_layout, null);
alertDialogBuilder.setView(dialogView);
// set dialog message
alertDialogBuilder
.setCancelable(false)
.setPositiveButton("SAVE",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
EditText userInput = (EditText) dialogView.findViewById(R.id.editText);
dbUtils.insertCSVFileName(userInput.getText().toString());
Log.d("hi","vaqlue of edittext" + userInput.getText().toString()); //gives me null
}
})
use only 1 method of the two, you have used both:
alertDialogBuilder.setView(dialogView); //good practice
alertDialogBuilder.setView(R.layout.dialog_layout);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.