【问题标题】:Attempt to invoke virtual method on Attempt to invoke virtual method android.widget.EditText.getText() on a null object reference尝试在空对象引用上调用虚拟方法 尝试在空对象引用上调用虚拟方法 android.widget.EditText.getText()
【发布时间】:2023-03-13 15:15:01
【问题描述】:

试图从对话框内的 EditText 中获取 Edit Text 的值,但一次又一次地得到这个错误

尝试调用虚拟方法'android.text.Editable android.widget.EditText.getText()' 在空对象引用上

我在另一个对话框上放大视图,它工作正常,但是每当我在对话框中单击“确定”时,我都会不断收到上述错误

LayoutInflater li = LayoutInflater.from(getContext());
                View promptsView = li.inflate(R.layout.custom_dimension_dialog, null);
                AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                        getContext());
                // set prompts.xml to alertdialog builder
                alertDialogBuilder.setView(promptsView);
                alertDialogBuilder
                        .setCancelable(false)
                        .setPositiveButton("OK",
                                new OnClickListener() {
                                    public void onClick(DialogInterface dialog, int id) {
                                        // get user input and set it to result
                                        // edit text
                                        widthEditText = findViewById(R.id.Width);
                                        heightEditText = findViewById(R.id.Height);
                                        String width = widthEditText.getText().toString();
                                        String height = heightEditText.getText().toString();
                                        Toast.makeText(mContext, "Values are: " + width + " " + height, Toast.LENGTH_SHORT).show();
//                                        WIDTH=width
//                                        HEIGHT=height;
                                    }
                                })
                        .setNegativeButton("Cancel",
                                new OnClickListener() {
                                    public void onClick(DialogInterface dialog, int id) {
                                        dialog.cancel();
                                    }
                                }).show(); 

【问题讨论】:

  • @Umar 检查我的答案,它会起作用
  • @MilanPansuriya 再次感谢伙计!
  • @Umar 接受我的回答

标签: android android-edittext android-alertdialog


【解决方案1】:

您没有提供任何视图来编辑文本以这种方式更改您的代码

 widthEditText = promptsView.findViewById(R.id.Width);                                 
 heightEditText = promptsView.findViewById(R.id.Height);

【讨论】:

    【解决方案2】:

    您需要从对话框视图中找到 EditText,例如。

    widthEditText = promptsView.findViewById(R.id.Width);
    heightEditText = promptsView.findViewById(R.id.Height);
    

    【讨论】:

      【解决方案3】:
      widthEditText = promptsView.findViewById(R.id.Width);
      heightEditText = promptsView.findViewById(R.id.Height);
      

      你需要以这种方式使用你的视图promptView.findviewbyId

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多