【问题标题】:EditText is null when inflating from custom dialog view从自定义对话框视图膨胀时,EditText 为空
【发布时间】:2012-10-12 15:32:57
【问题描述】:

我正在尝试从 EditText 中获取文本,但我已经注销了 EditText 并且它为空。我试图从自定义视图中对其进行充气。我是不是吹错了?

我知道 prompsView 是我的自定义 AlertDialog 视图,其中是我的 EditText。也许我应该在充气时传入prompsView,只是不知道该怎么做。

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();
    }

【问题讨论】:

    标签: android view android-alertdialog layout-inflater


    【解决方案1】:

    试试:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-05-16
      • 1970-01-01
      • 1970-01-01
      • 2014-08-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多