I have problem with getting data, that user inputs in dialog
(with custom layout). Look at the code:
unit.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</EditText>
</RelativeLayout>
code:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
LayoutInflater inflater = this.getLayoutInflater();
builder.setView(inflater.inflate(R.layout.unit, null));
AlertDialog dialog = builder.create();
dialog.show();
Dialog
opens with layout I have created, but that
builder.setPositiveButton("Add", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
EditText text = (EditText)findViewById(R.id.editText1);
Toast.makeText(getApplicationContext(), text.getText(), Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
});
produces runtime error: java.lang.NullPointerException
. Where is the problem?
use this..
builder.setPositiveButton("Add", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
EditText text = (EditText) dialog.findViewById(R.id.editText1);
Toast.makeText(getApplicationContext(), text.getText(), Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With