我无法弄清楚我的代码出错了。每次按下按钮我的应用程序都会关闭,除非我添加一个尝试并捕获。一旦我输入两个数字并添加符号,结果应该打印出来,但应用程序只是关闭。这是我的LogCat和代码。顺便说一下,我是java&的新手。 android studio,请原谅我的错误。
public class MainActivity extends AppCompatActivity {
Button btnAdd, btnSubtract,btnMultiply, btnDivide;
TextView textResults, textSigns;
EditText firstnumber, secondnumber;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
firstnumber = (EditText) findViewById(R.id.firstnumber);
secondnumber =(EditText) findViewById(R.id.secondnumber);
textResults = (TextView) findViewById(R.id.results);
textSigns = (TextView) findViewById(R.id.invisible_signs);
btnAdd = (Button) findViewById(R.id.Add);
btnDivide = (Button) findViewById(R.id.divide);
btnMultiply =(Button) findViewById(R.id.Multiply);
btnSubtract =(Button) findViewById(R.id.Subtract);
final String number1 = firstnumber.getText().toString();
final String number2 = secondnumber.getText().toString();
btnDivide.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {int divide = Integer.parseInt(number1) / Integer.parseInt(number2);
textResults.setText(number1 + " / " + number2 );
textResults.setVisibility(View.VISIBLE);
textResults.append("\n" + divide);}
catch(Exception e){
Toast.makeText(getApplicationContext(), "Cannot Divide These Numbers", Toast.LENGTH_SHORT).show();
}
}
});
btnMultiply.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int multiply = Integer.parseInt(number1) * Integer.parseInt(number2);
textResults.setText(number1 + " * " + number2 );
textResults.setVisibility(View.VISIBLE);
textResults.append("\n" + multiply);
}
});
btnAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int addition = Integer.parseInt(number1) + Integer.parseInt(number2);
textResults.setText(number1 + " + " + number2 );
textResults.setVisibility(View.VISIBLE);
textResults.append("\n" + addition);
}
});
btnSubtract.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int subtract = Integer.parseInt(number1) - Integer.parseInt(number2);
textResults.setText(number1 + " - " + number2 );
textResults.setVisibility(View.VISIBLE);
textResults.append("\n" + subtract);
}
});
}
}
。 logcat的:
0-25 16:12:28.330 1860-1860/com.squarespace.atpublishing.calculatorapp I/art: Not late-enabling -Xcheck:jni (already on)
10-25 16:12:28.640 1860-1867/com.squarespace.atpublishing.calculatorapp E/art: Failed writing handshake bytes (-1 of 14): Broken pipe
10-25 16:12:28.640 1860-1867/com.squarespace.atpublishing.calculatorapp I/art: Debugger is no longer active
10-25 16:12:29.680 1860-1872/com.squarespace.atpublishing.calculatorapp W/art: Suspending all threads took: 20ms
10-25 16:12:29.680 1860-1872/com.squarespace.atpublishing.calculatorapp I/art: Background partial concurrent mark sweep GC freed 371(64KB) AllocSpace objects, 0(0B) LOS objects, 47% free, 556KB/1068KB, paused 20ms total 280ms
10-25 16:12:30.370 1860-1867/com.squarespace.atpublishing.calculatorapp W/art: Suspending all threads took: 40ms
10-25 16:12:30.620 1860-1900/com.squarespace.atpublishing.calculatorapp D/OpenGLRenderer: Render dirty regions requested: true
10-25 16:12:30.690 1860-1860/com.squarespace.atpublishing.calculatorapp D/: HostConnection::get() New Host Connection established 0x7fc39eccf580, tid 1860
10-25 16:12:30.690 1860-1860/com.squarespace.atpublishing.calculatorapp D/Atlas: Validating map...
10-25 16:12:30.870 1860-1867/com.squarespace.atpublishing.calculatorapp W/art: Suspending all threads took: 100ms
10-25 16:12:30.890 1860-1872/com.squarespace.atpublishing.calculatorapp I/art: Background sticky concurrent mark sweep GC freed 824(47KB) AllocSpace objects, 0(0B) LOS objects, 13% free, 919KB/1068KB, paused 0 total 160ms
10-25 16:12:30.940 1860-1872/com.squarespace.atpublishing.calculatorapp W/art: Suspending all threads took: 50ms
10-25 16:12:31.300 1860-1867/com.squarespace.atpublishing.calculatorapp W/art: Suspending all threads took: 10ms
10-25 16:12:31.540 1860-1872/com.squarespace.atpublishing.calculatorapp I/art: Background sticky concurrent mark sweep GC freed 437(19KB) AllocSpace objects, 0(0B) LOS objects, 11% free, 942KB/1068KB, paused 0 total 240ms
10-25 16:12:31.680 1860-1900/com.squarespace.atpublishing.calculatorapp I/OpenGLRenderer: Initialized EGL, version 1.4
10-25 16:12:31.720 1860-1900/com.squarespace.atpublishing.calculatorapp D/OpenGLRenderer: Enabling debug mode 0
10-25 16:12:31.760 1860-1900/com.squarespace.atpublishing.calculatorapp W/EGL_emulation: eglSurfaceAttrib not implemented
10-25 16:12:31.760 1860-1900/com.squarespace.atpublishing.calculatorapp W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0x7fc39ece3040, error=EGL_SUCCESS
10-25 20:12:38.850 1860-1867/com.squarespace.atpublishing.calculatorapp W/art: Suspending all threads took: 220ms
10-25 20:12:39.010 1860-1860/com.squarespace.atpublishing.calculatorapp I/Choreographer: Skipped 72 frames! The application may be doing too much work on its main thread.
10-25 20:12:39.790 1860-1860/com.squarespace.atpublishing.calculatorapp I/Choreographer: Skipped 46 frames! The application may be doing too much work on its main thread.
10-25 16:12:42.610 1860-1867/com.squarespace.atpublishing.calculatorapp W/art: Suspending all threads took: 30ms
10-25 16:12:42.940 1860-1860/com.squarespace.atpublishing.calculatorapp I/Choreographer: Skipped 37 frames! The application may be doing too much work on its main thread.
10-25 16:12:43.120 1860-1900/com.squarespace.atpublishing.calculatorapp W/EGL_emulation: eglSurfaceAttrib not implemented
10-25 16:12:43.120 1860-1900/com.squarespace.atpublishing.calculatorapp W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0x7fc3a5ff6fc0, error=EGL_SUCCESS
10-25 16:12:49.020 1860-1867/com.squarespace.atpublishing.calculatorapp W/art: Suspending all threads took: 150ms
10-25 16:12:49.380 1860-1867/com.squarespace.atpublishing.calculatorapp W/art: Suspending all threads took: 80ms
10-25 16:12:58.690 1860-1860/com.squarespace.atpublishing.calculatorapp D/AndroidRuntime: Shutting down VM
10-25 16:12:58.770 1860-1860/com.squarespace.atpublishing.calculatorapp E/AndroidRuntime: FATAL EXCEPTION: main
10-25 16:12:58.770 1860-1860/com.squarespace.atpublishing.calculatorapp E/AndroidRuntime: Process: com.squarespace.atpublishing.calculatorapp, PID: 1860
10-25 16:12:58.770 1860-1860/com.squarespace.atpublishing.calculatorapp E/AndroidRuntime: java.lang.NumberFormatException: Invalid int: ""
10-25 16:12:58.770 1860-1860/com.squarespace.atpublishing.calculatorapp E/AndroidRuntime: at java.lang.Integer.invalidInt(Integer.java:138)
10-25 16:12:58.770 1860-1860/com.squarespace.atpublishing.calculatorapp E/AndroidRuntime: at java.lang.Integer.parseInt(Integer.java:358)
10-25 16:12:58.770 1860-1860/com.squarespace.atpublishing.calculatorapp E/AndroidRuntime: at java.lang.Integer.parseInt(Integer.java:334)
10-25 16:12:58.770 1860-1860/com.squarespace.atpublishing.calculatorapp E/AndroidRuntime: at com.squarespace.atpublishing.calculatorapp.MainActivity$2.onClick(MainActivity.java:56)
10-25 16:12:58.770 1860-1860/com.squarespace.atpublishing.calculatorapp E/AndroidRuntime: at android.view.View.performClick(View.java:4756)
10-25 16:12:58.770 1860-1860/com.squarespace.atpublishing.calculatorapp E/AndroidRuntime: at android.view.View$PerformClick.run(View.java:19749)
10-25 16:12:58.770 1860-1860/com.squarespace.atpublishing.calculatorapp E/AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:739)
10-25 16:12:58.770 1860-1860/com.squarespace.atpublishing.calculatorapp E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:95)
10-25 16:12:58.770 1860-1860/com.squarespace.atpublishing.calculatorapp E/AndroidRuntime: at android.os.Looper.loop(Looper.java:135)
10-25 16:12:58.770 1860-1860/com.squarespace.atpublishing.calculatorapp E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5221)
10-25 16:12:58.770 1860-1860/com.squarespace.atpublishing.calculatorapp E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
10-25 16:12:58.770 1860-1860/com.squarespace.atpublishing.calculatorapp E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:372)
10-25 16:12:58.770 1860-1860/com.squarespace.atpublishing.calculatorapp E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
10-25 16:12:58.770 1860-1860/com.squarespace.atpublishing.calculatorapp E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
10-25 16:13:05.160 1860-1867/com.squarespace.atpublishing.calculatorapp W/art: Suspending all threads took: 250ms
答案 0 :(得分:0)
在进行计算时,您不会读取输入的值,因此字符串将始终为空。
变量在分配时得到它们的值,赋值并不意味着它们与输入绑定并在输入改变时改变它们的值。
答案 1 :(得分:0)
删除这些行
final String number1 = firstnumber.getText().toString();
final String number2 = secondnumber.getText().toString();
并调整此
int divide = Integer.parseInt(firstnumber.getText()) / Integer.parseInt(secondnumber.getText());