The topic explains what i'm after... I can't retrieve the EditText from my custom view in android. All I get is a Nullpointer Exception. :/ I've marked where the problems are in the code with comments. The ID:s are correct and my XML layout is a simple RelativeLayout containing two EditText attributes. Obviously I'm missing something trivial here, but I've now stared at the code for almost 2 hours without solving this, so I thought I'll give SO a try instead.
protected void showLoginDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
// Get the layout inflater
LayoutInflater inflater = this.getLayoutInflater();
// Inflate and set the layout for the dialog
// Pass null as the parent view because its going in the dialog layout
builder.setView(inflater.inflate(R.layout.activity_login, null))
// Add action buttons
.setPositiveButton(R.string.login, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
/* ERROR HERE! */
EditText uName, passWord;
uName = (EditText) findViewById(R.id.login_username);
passWord = (EditText) findViewById(R.id.login_password);
Log.i(TAG, uName.getText().toString() + " " + passWord.getText().toString());
/* STOP */
if(the_view.getSocketTask().isConnected) {
the_view.getSocketTask().send_command("LOGIN ");
} else {
showToast("Not connected!");
}
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
}
});
builder.create().show();
}
EDIT:
After suggestions the following code is a working one! Thanks again!
protected void showLoginDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
// Get the layout inflater
LayoutInflater inflater = this.getLayoutInflater();
// Inflate and set the layout for the dialog
// Pass null as the parent view because its going in the dialog layout
builder.setView(inflater.inflate(R.layout.activity_login, null))
// Add action buttons
.setPositiveButton(R.string.login, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Dialog f = (Dialog) dialog;
/* ERROR HERE! */
EditText uName, passWord;
uName = (EditText) f.findViewById(R.id.login_username);
passWord = (EditText) f.findViewById(R.id.login_password);
Log.i(TAG, uName.getText().toString() + " " + passWord.getText().toString());
/* STOP */
if(the_view.getSocketTask().isConnected) {
the_view.getSocketTask().send_command("LOGIN ");
} else {
showToast("Not connected!");
}
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
}
});
builder.create().show();
}
Thanks in advance! Alex
Cast the Dialog as a View:
View v_iew=inflater.inflate(R.layout.activity_login, null)) ;
builder.setView(v_iew);
Then replace:
uName = (EditText) findViewById(R.id.login_username);
passWord = (EditText) findViewById(R.id.login_password);
with
uName = (EditText) v_iew.findViewById(R.id.login_username);
passWord = (EditText) v_iew.findViewById(R.id.login_password);
(Can't vote so creating new answer: Took me hours to figure this out; adding the qualifier finally fixed it - like you figure out already)
Doesnt work: final Dialog dialog = new Dialog(this); ... keyInput = (EditText) findViewById(R.id.key_input);
Works: final Dialog dialog = new Dialog(this); ... keyInput = (EditText) dialog.findViewById(R.id.key_input);
In the case of an EditText you should implement TextWatcher It is generally a very bad idea to use editText.getText()
Here is a very simple example code for Custom Dialog containing an EditText as part of the layout. There is also a button that needs to be on the same layout which when clicked will show you the text which you just entered. Have fun!
final String inputString = null;
final Dialog dialog = new Dialog(YourActivityName.this);
dialog.setContentView(R.layout.custom_dialog_layout);
EditText editText = (EditText) dialog.findViewById(R.id.id_of_edit_text);
Button done = (Button) dialog.findViewById(R.id.done);
dialog.show();
editText.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
inputString = s.toString();
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});
done.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(YourActivityName.this, inputString, Toast.LENGTH_SHORT);
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