read user Input of custom dialogs

Posted by urobo on Stack Overflow See other posts from Stack Overflow or by urobo
Published on 2010-05-23T14:28:26Z Indexed on 2010/05/23 14:30 UTC
Read the original article Hit count: 503

Filed under:
|

I built a custom dialog starting from an AlertDialog to obtain login information from a user. So the dialog contains two EditText fields, using the layoutinflater service I obtain the layout and I'm saving a reference to the fields.

LayoutInflater inflater = (LayoutInflater) Home.this.getSystemService(LAYOUT_INFLATER_SERVICE);
layoutLogin = inflater.inflate(R.layout.login,(ViewGroup) findViewById(R.id.rl));
usernameInput =((EditText)findViewById(R.id.getNewUsername));
passwordInput = ((EditText)findViewById(R.id.getNewPassword));

Then I have my overridden onCreateDialog(...) :

{
AlertDialog d = null;
AlertDialog.Builder builder = new AlertDialog.Builder(this);

    switch(id){
    ...
    case Home.DIALOG_LOGIN:

        builder.setView(layoutLogin);
        builder.setMessage("Sign in to your DyCaPo Account").setCancelable(false);
        d=builder.create();
        d.setTitle("Login");
        Message msg = new Message();
        msg.setTarget(Home.this.handleLogin);
        Bundle data = new Bundle();

        data.putString("username", usernameInput.getText().toString());// <---null pointer Exception
        data.putString("password", passwordInput.getText().toString());
        msg.setData(data);
        d.setButton(Dialog.BUTTON_NEUTRAL,"Sign in",msg);
        break;
...
return d;
}

and the handler set in the Message:

private Handler handleLogin= new Handler(){

    /* (non-Javadoc)
     * @see android.os.Handler#handleMessage(android.os.Message)
     */
    @Override
    public void handleMessage(Message msg) {
        // TODO Auto-generated method stub
        Log.d("Message Received", msg.getData().getString("username")+ msg.getData().getString("password"));
    }

};

which for now works as a debugging tool.

That's all. The Question is: what am I doing wrong? Because when I reach the line highlighted in the code ( the line in which I read the fields in the dialog ) I always get a null pointer exception. Could somebody please tell me the reason why it is so? And give some guidelines to work with dialogs. Thanks in advance!

© Stack Overflow or respective owner

Related posts about android

Related posts about dialogs