turtleboy
turtleboy

Reputation: 7572

EditText is null when inflating from custom dialog view

I'm trying get the text from an EditText but i've logged out the EditText and it's null. I've tried to inflate it from a custom view. Am i inflating it incorrectly?

I know prompsView is my custom AlertDialog view and within that is my EditText. Maybe i should pass in the prompsView when inflating, just don't know how to do that.

public void showPasswordDialogBox(){

        LayoutInflater li = LayoutInflater.from(NfcscannerActivity.this);
        View promptsView = li.inflate(R.layout.passwordpromptdialogboxlayout, null);

        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                NfcscannerActivity.this);

        // set prompts.xml to alertdialog builder
        alertDialogBuilder.setView(promptsView);



        // set dialog message
        alertDialogBuilder
            .setCancelable(false)
            .setPositiveButton("OK",
              new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog,int id) {
                    EditText passwordPin = (EditText)findViewById(R.id.passwordprompttextview);
                    Log.e(TAG, "edittext is null ? " + passwordPin);
                    String passPin = passwordPin.getText().toString();
                    Log.e(TAG, "password from edittext = " + passPin);

                    Cursor c = nfcscannerapplication.loginValidate.queryAllFromCarer();
                    if(c != null){
                        c.moveToLast();
                        String passPinFromDB =  c.getString(c
                                .getColumnIndex(LoginValidate.C_PASSWORD));

                        if (passPin.trim().equalsIgnoreCase(passPinFromDB)){

                            Log.e(TAG, "passwords match");
                            DateTime now = new DateTime();
                            DateTimeFormatter fmt = DateTimeFormat.forPattern("d-MMM-Y");
                            String formattedNow = fmt.print(now);
                            String[] params = new String[]{carerID, formattedNow}; 
                            AsyncGetRota agr = new AsyncGetRota();
                            agr.execute(params);

                        }else{
                            Toast.makeText(NfcscannerActivity.this,
                                    "Please check Password/Pin",
                                    Toast.LENGTH_LONG).show();
                            showPasswordDialogBox();
                        }

                    }else{
                        Log.e(TAG, "cursor null while trying to verify password in showPasswordDialogBox() ");
                    }

                }
              })
            .setNegativeButton("Cancel",
              new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog,int id) {
                dialog.cancel();
                }
              });

        // create alert dialog
        AlertDialog alertDialog = alertDialogBuilder.create();



        // show it
        alertDialog.show();
    }

Upvotes: 0

Views: 1130

Answers (1)

Joss Stuart
Joss Stuart

Reputation: 1886

Try:

EditText passwordPin = (EditText) promptsView.findViewById(R.id.passwordprompttextview);

Upvotes: 2

Related Questions