gpt4 book ai didi

android - Attempt to invoke virtual method on Attempt to invoke virtual method android.widget.EditText.getText() 对空对象引用

转载 作者:搜寻专家 更新时间:2023-11-01 08:20:58 26 4
gpt4 key购买 nike

尝试从对话框中的 EditText 获取 Edit Text 的值,但一次又一次地出现此错误

Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference

我正在通过另一个对话框膨胀 View 它工作正常但是每当我在对话框中单击“确定”时我总是收到上述错误

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

最佳答案

您没有为 edittext 提供任何 View 以这种方式更改您的代码

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

关于android - Attempt to invoke virtual method on Attempt to invoke virtual method android.widget.EditText.getText() 对空对象引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51196536/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com