Java Android Studio错误:尝试在空对象引用上调用虚拟方法“Android.text.Editable Android.widget.EditText.getText()”
我一直在尝试为我的应用程序创建这个自定义对话框窗口,当按下该窗口并请求用户的数字输入时,它将弹出。我以前已经测试过这个自定义对话框,它在过去成功地工作过,但是当我在本例中尝试它时,它不断抛出错误,试图在一个空对象引用上调用虚拟方法“android.text.Editable android.widget.EditText.getText”Java Android Studio错误:尝试在空对象引用上调用虚拟方法“Android.text.Editable Android.widget.EditText.getText()”,java,android,Java,Android,我一直在尝试为我的应用程序创建这个自定义对话框窗口,当按下该窗口并请求用户的数字输入时,它将弹出。我以前已经测试过这个自定义对话框,它在过去成功地工作过,但是当我在本例中尝试它时,它不断抛出错误,试图在一个空对象引用上调用虚拟方法“android.text.Editable android.widget.EditText.getText” final EditText inputNumber = input_score.findViewById(R.id.inputNumber); number
final EditText inputNumber = input_score.findViewById(R.id.inputNumber);
numberOfPoints = Integer.parseInt(inputNumber.getText().toString());
当按下按钮打开自定义弹出窗口时,此错误将导致屏幕在返回应用程序主菜单前几秒钟断电。应该发生的是弹出对话框窗口以获取用户的输入
private void openDialog() {
input_score = new Dialog(this);
beginButton = findViewById(R.id.beginButton);
final EditText inputNumber = input_score.findViewById(R.id.inputNumber);
numberOfPoints = Integer.parseInt(inputNumber.getText().toString());
input_score.setContentView(R.layout.input_score);
input_score.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
input_score.show();
beginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (inputNumber.getText().toString().trim().isEmpty()) {
//change textview to error for a few seconds before returning to normal
} else {
if (Integer.parseInt(inputNumber.getText().toString()) <= 0 || Integer.parseInt(inputNumber.getText().toString()) > 900) {
startActivity(new Intent(MainMenu.this, MainGame.class));
}
}
}
});
我不确定我该如何准确地解决这个问题,所以请指出代码中的错误,以改进和解决它。多谢各位
编辑:正如@Shay Kin所展示的,他使用了一个onShowListener,然后将按钮侦听器放在其中,这样它就会知道这个对话框何时会弹出,然后同时弹出按钮函数。在我的例子中,代码不断中断的原因不仅是因为.show的位置错误,还因为我有一个变量numberOfPoints,它不断解析一个空文本字段并中断代码,因为里面没有值。我没有注意到这一点,直到我再次深入研究并删除了该变量
我所做的修复代码的工作是实现他的解决方案,该解决方案已经将buttonListener合并到其中,并删除numberOfPoints变量。这导致程序被修复并成功运行可能是因为EditText位于不同的范围?尝试在其他组件旁边的函数外部声明EditText。onClick函数是一个回调函数,因此在openDialog完成执行后,可能会处理EditText。在调用Dialog.show之前,视图还不存在,因此findViewById找不到所需的视图,而是返回null。这些视图只有在调用show或create之后才可用
因为您实际上事先并不需要它,所以可以将此初始化移动到实际需要它的位置
beginButton.SetOnClickListener新建视图.OnClickListener{
@凌驾
公共视图v{
//在这里初始化它
final EditText inputNumber=输入\分数.findViewByIdR.id.inputNumber;
如果inputNumber.getText.toString.trim.isEmpty{
//将textview更改为error几秒钟,然后返回正常状态
}否则{
如果Integer.parseIntinputNumber.getText.toString 900{
startActivitynew IntentMainMenu.this、MainGame.class;
}
}
}
};
但是,必须指出的是,您应该避免直接使用Dialog类。正如@Gabriel所说,您得到了NullPointerException,因为您在显示对话框后初始化了EditText,这样您就可以添加侦听器,当对话框在此处可见时,您可以初始化所有视图
private void openDialog() {
input_score = new Dialog(this);
input_score.setContentView(R.layout.speach_dialog);
input_score.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
input_score.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface dialog) {
beginButton = findViewById(R.id.beginButton);
final EditText inputNumber = input_score.findViewById(R.id.inputNumber);
numberOfPoints = Integer.parseInt(inputNumber.getText().toString());
beginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (inputNumber.getText().toString().trim().isEmpty()) {
//change textview to error for a few seconds before returning to normal
} else {
if (Integer.parseInt(inputNumber.getText().toString()) <= 0 || Integer.parseInt(inputNumber.getText().toString()) > 900) {
startActivity(new Intent(MainMenu.this, MainGame.class));
}
}
}
});
}
});
input_score.show();
}
哦,我明白了,所以在那之前我只是引用了一些东西,因此出现了错误,我不知道我必须在创建之后初始化,因为我认为它不会做任何事情,但我现在明白了。无论如何,谢谢你的帮助,Gabriel和thisiszammy。虽然我想指出一些事情,但在使用解决方案时,另一个问题是,按beginButton实际上并没有按照预期移动到主游戏类。这些条件也不起作用,因为当数字大于900或为空时,它们不会释放错误。啊,是的,开始按钮不属于对话框,我犯了一个错误,所以你在你的活动中声明了它。哦,不,不,按钮属于对话框,事实上,当按下按钮在游戏中弹出对话框窗口时,它与对话框一起出现,只是我不确定为什么它不起作用。我确实采纳了您的建议,将按钮侦听器移出了对话框,但仍然在openDialog函数中,但我最终得到了与以前相同的错误。我对此有些陌生,所以我只是从这些错误中学习如果在对话框中使用beginButton=input_score.findViewByIdR.id.beginButton;而不是beginButton=findViewByIdR.id.beginButton;
private void openDialog() {
input_score = new Dialog(this);
input_score.setContentView(R.layout.speach_dialog);
input_score.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
input_score.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface dialog) {
beginButton = findViewById(R.id.beginButton);
final EditText inputNumber = input_score.findViewById(R.id.inputNumber);
numberOfPoints = Integer.parseInt(inputNumber.getText().toString());
beginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (inputNumber.getText().toString().trim().isEmpty()) {
//change textview to error for a few seconds before returning to normal
} else {
if (Integer.parseInt(inputNumber.getText().toString()) <= 0 || Integer.parseInt(inputNumber.getText().toString()) > 900) {
startActivity(new Intent(MainMenu.this, MainGame.class));
}
}
}
});
}
});
input_score.show();
}