I am in an impasse regarding getting the text from a custom AlertDialog. I get an error "NullPointerException". I have moved defining the variable containing the EditText in the AlertDialog, but I get the same error.
My layout item in it's XML "pin.xml"
<EditText
android:id="@+id/insert_pin"
android:layout_width="90dp"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:ems="10"
android:gravity="center"
android:inputType="numberPassword"
android:maxLength="4" />
The AlertDialog
new AlertDialog.Builder(this)
.setView(inflater.inflate(R.layout.pin, null))
.setTitle("Save PIN")
.setPositiveButton("Save", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int id){
pin = (EditText) findViewById(R.id.insert_pin);
//here I get the Error. Apparently, it can't get the value
input = pin.getText().toString();
dialog.cancel();
go();
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int id){
dialog.cancel();
finish();
}
})
.show();
Any help would be vastly appreciated.
use
for getting text from insert_pin
EditText
you will need to use dialog contextwrite your code like this
you have to change your code as you are find the EditText Field object using findViewById() but you should findViewById() with respect to Dialog View.
Change your code as below:
Use this: