Android vm 中的应用程序突然停止。 尝试在空对象引用上调用虚拟方法 'android.text.Editable android.widget.EditText.getText()'
请帮忙。 安卓工作室错误 错误:
2021-01-08 00:58:49.288 9583-9583/com.example.simplecalculator E/Android运行时:致命异常:main 进程: com.example.simplecalculator, PID: 9583 java.lang.IllegalStateException: 无法执行 android:onClick 的方法 at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:402) at android.view.View.performClick(View.java:7125) at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:967) at android.view.View.performClickInternal(View.java:7102) at android.view.view.access$3500(view.java:801) at android.view.View$PerformClick.run(View.java:27336) at android.os.Handler.handleCallback(Handler.java:883) at android.os.Handler.dispatchMessage(Handler.java:100) at android.os.Looper.loop(Looper.java:214) at android.app.ActivityThread.main(ActivityThread.java:7356) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930) 由: java.lang.reflect.InvocationTargetException at java.lang.reflect.Method.invoke(Native Method) at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:397) at android.view.View.performClick(View.java:7125) at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:967) at android.view.View.performClickInternal(View.java:7102) at android.view.view.access$3500(view.java:801) at android.view.View$PerformClick.run(View.java:27336) at android.os.Handler.handleCallback(Handler.java:883) at android.os.Handler.dispatchMessage(Handler.java:100) at android.os.Looper.loop(Looper.java:214) at android.app.ActivityThread.main(ActivityThread.java:7356) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930) 由:java.lang.NullPointerException:尝试调用虚拟方法'android.text.Editable android.widget.EditText.getText()' 在空对象引用上 at com.example.simplecalculator.MainActivity.sum(MainActivity.java:33) at java.lang.reflect.Method.invoke(Native Method) at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:397) at android.view.View.performClick(View.java:7125) at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:967) at android.view.View.performClickInternal(View.java:7102) at android.view.view.access$3500(view.java:801) at android.view.View$PerformClick.run(View.java:27336) at android.os.Handler.handleCallback(Handler.java:883) at android.os.Handler.dispatchMessage(Handler.java:100) at android.os.Looper.loop(Looper.java:214) at android.app.ActivityThread.main(ActivityThread.java:7356) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930) 2021-01-08 00:58:49.336 9583-9583/com.example.simplecalculator I/进程:发送信号。PID: 9583 SIG: 9
也添加我的代码。
public class MainActivity extends AppCompatActivity {
EditText num1Text;
EditText num2Text;
TextView ResText;
int num1;
int num2;
int result;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText num1Text =(EditText) findViewById(R.id.num1Text);
EditText num2Text =(EditText) findViewById(R.id.num2Text);
TextView ResText =(TextView) findViewById(R.id.ResText);
}
public void sum(View view) {
if(num1Text.getText().toString().matches("") || num2Text.getText().toString().matches("")){
ResText.setText("Enter number");
}
else{
num1 = Integer.parseInt(num1Text.getText().toString());
num2 = Integer.parseInt(num2Text.getText().toString());
result = num1 + num2;
ResText.setText("Result = " + result);
}
}
public void deduct(View view) {
if(num1Text.getText().toString().matches("") || num2Text.getText().toString().matches("")){
ResText.setText("Enter number");
}
else {
num1 = Integer.parseInt(num1Text.getText().toString());
num2 = Integer.parseInt(num2Text.getText().toString());
result = (num1 - num2);
ResText.setText("Result = " + result);
}
}
public void multiply(View view) {
if(num1Text.getText().toString().matches("") || num2Text.getText().toString().matches("")){
ResText.setText("Enter number");
}
else {
num1 = Integer.parseInt(num1Text.getText().toString());
num2 = Integer.parseInt(num2Text.getText().toString());
result = num1 * num2;
ResText.setText("Result = " + result);
}
}
public void divide(View view) {
if(num1Text.getText().toString().matches("") || num2Text.getText().toString().matches("")){
ResText.setText("Enter number");
}
else {
num1 = Integer.parseInt(num1Text.getText().toString());
num2 = Integer.parseInt(num2Text.getText().toString());
result = num1 / num2;
ResText.setText("Result = " + result);
}
}
}
您没有正确初始化EditText
和TextView
字段。
您的代码实际执行的是使用 findViewById 获取值,然后将它们分配给局部变量而不是相应的字段。
改变
EditText num1Text = (EditText) findViewById(R.id.num1Text);
EditText num2Text = (EditText) findViewById(R.id.num2Text);
TextView ResText = (TextView) findViewById(R.id.ResText);
自
num1Text = (EditText) findViewById(R.id.num1Text);
num2Text = (EditText) findViewById(R.id.num2Text);
ResText = (TextView) findViewById(R.id.ResText);
值得注意的是,还有其他错误。 例如,您不处理用户在其中一个文本框中输入非数字值的情况。 当您尝试将文本转换为数字时,这将导致异常。