我对 Java 和 Android 应用程序非常陌生,我正在创建一个简单的计算器,一切进展顺利,但我发现了一个问题,那就是当我按下例如乘以没有数字时,我的模拟器会崩溃,我错过了一些在我的听众中有点鳕鱼,但无法找出它的位置和位置是我的 MainActivity,Java:
package com.firstprogram;
import android.os.Bundle;
import android.app.Activity;
import android.text.InputType;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MainActivity extends Activity {
LinearLayout layout1;
LinearLayout buttonLayout;
EditText number1text;
EditText number2text;
Button multiplyButton;
Button divideButton;
Button addButton;
Button subButton;
TextView answerText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
layout1 = new LinearLayout (this);
buttonLayout = new LinearLayout (this);
number1text = new EditText (this);
number2text = new EditText (this);
multiplyButton = new Button (this);
divideButton = new Button (this);
addButton = new Button (this);
subButton = new Button (this);
answerText = new TextView (this);
answerText.setTextSize(TypedValue.COMPLEX_UNIT_SP,28);
layout1.setOrientation(LinearLayout.VERTICAL);
layout1.setGravity(Gravity.CENTER_HORIZONTAL);
buttonLayout.setOrientation(LinearLayout.HORIZONTAL);
buttonLayout.setGravity(Gravity.CENTER_HORIZONTAL);
number1text.setInputType(InputType.TYPE_CLASS_NUMBER);
number2text.setInputType(InputType.TYPE_CLASS_NUMBER);
answerText.setGravity(Gravity.CENTER_HORIZONTAL);
answerText.setText("0");
multiplyButton.setText("X");
divideButton.setText("/");
addButton.setText("+");
subButton.setText("-");
multiplyButton.setOnClickListener((android.view.View.OnClickListener) multiplyClicked);
divideButton.setOnClickListener((android.view.View.OnClickListener) divideClicked);
addButton.setOnClickListener((android.view.View.OnClickListener) addClicked);
subButton.setOnClickListener((android.view.View.OnClickListener) subClicked);
buttonLayout.addView(multiplyButton);
buttonLayout.addView(divideButton);
buttonLayout.addView(addButton);
buttonLayout.addView(subButton);
layout1.addView(number1text);
layout1.addView(number2text);
layout1.addView(buttonLayout);
layout1.addView(answerText);
setContentView(layout1);
number1text.setLayoutParams(new LinearLayout.LayoutParams(500,50));
number2text.setLayoutParams(new LinearLayout.LayoutParams(500,50));
multiplyButton.setLayoutParams(new LinearLayout.LayoutParams(50,50));
divideButton.setLayoutParams(new LinearLayout.LayoutParams(50,50));
addButton.setLayoutParams(new LinearLayout.LayoutParams(50,50));
subButton.setLayoutParams(new LinearLayout.LayoutParams(50,50));
}
private View.OnClickListener multiplyClicked = new View.OnClickListener() {
@Override
public void onClick (View v){
String firstString = number1text.getText().toString();
String secondString = number2text.getText().toString();
double firstNumber = Double.parseDouble(firstString);
double secondNumber = Double.parseDouble(secondString);
double result = firstNumber * secondNumber;
String resultString = String.valueOf(result);
answerText.setText(resultString);
}
};
private View.OnClickListener divideClicked = new View.OnClickListener() {
@Override
public void onClick (View v){
String firstString = number1text.getText().toString();
String secondString = number2text.getText().toString();
double firstNumber = Double.parseDouble(firstString);
double secondNumber = Double.parseDouble(secondString);
if(secondNumber!=0) {
double result = firstNumber / secondNumber;
String resultString = String.valueOf(result);
answerText.setText(resultString);
}
}
};
private View.OnClickListener addClicked = new View.OnClickListener() {
@Override
public void onClick (View v){
String firstString = number1text.getText().toString();
String secondString = number2text.getText().toString();
double firstNumber = Double.parseDouble(firstString);
double secondNumber = Double.parseDouble(secondString);
double result = firstNumber + secondNumber;
String resultString = String.valueOf(result);
answerText.setText(resultString);
}
};
private View.OnClickListener subClicked = new View.OnClickListener() {
@Override
public void onClick (View v){
String firstString = number1text.getText().toString();
String secondString = number2text.getText().toString();
double firstNumber = Double.parseDouble(firstString);
double secondNumber = Double.parseDouble(secondString);
double result = firstNumber - secondNumber;
String resultString = String.valueOf(result);
answerText.setText(resultString);
}
};
}
这是日志猫:
08-23 07:15:06.241: D/gralloc_goldfish(792): Emulator without GPU emulation detected.
08-23 07:15:15.631: D/dalvikvm(792): GC_FOR_ALLOC freed 257K, 14% free 2762K/3176K, paused 50ms, total 63ms
08-23 07:20:06.893: D/gralloc_goldfish(850): Emulator without GPU emulation detected.
08-23 07:21:21.171: I/Choreographer(850): Skipped 33 frames! The application may be doing too much work on its main thread.
08-23 07:21:30.881: I/Choreographer(850): Skipped 31 frames! The application may be doing too much work on its main thread.
08-23 07:23:39.641: D/gralloc_goldfish(893): Emulator without GPU emulation detected.
08-23 07:29:47.051: I/Choreographer(893): Skipped 42 frames! The application may be doing too much work on its main thread.
08-23 07:37:02.152: I/Choreographer(893): Skipped 35 frames! The application may be doing too much work on its main thread.
08-23 07:38:16.154: D/gralloc_goldfish(936): Emulator without GPU emulation detected.
08-23 07:51:40.516: I/Choreographer(936): Skipped 72 frames! The application may be doing too much work on its main thread.
08-23 07:53:47.001: I/Choreographer(936): Skipped 58 frames! The application may be doing too much work on its main thread.
08-23 08:22:32.431: I/Choreographer(936): Skipped 32 frames! The application may be doing too much work on its main thread.
08-23 08:36:53.827: I/Choreographer(936): Skipped 54 frames! The application may be doing too much work on its main thread.
08-23 08:51:51.864: I/Choreographer(936): Skipped 54 frames! The application may be doing too much work on its main thread.
08-23 08:56:47.101: I/Choreographer(936): Skipped 34 frames! The application may be doing too much work on its main thread.
08-23 08:57:40.044: I/Choreographer(936): Skipped 42 frames! The application may be doing too much work on its main thread.
08-23 09:07:14.461: I/Choreographer(936): Skipped 48 frames! The application may be doing too much work on its main thread.
08-23 09:08:47.069: I/Choreographer(936): Skipped 32 frames! The application may be doing too much work on its main thread.
08-23 09:14:26.301: D/gralloc_goldfish(1109): Emulator without GPU emulation detected.
08-23 09:14:31.151: D/AndroidRuntime(1109): Shutting down VM
08-23 09:14:31.151: W/dalvikvm(1109): threadid=1: thread exiting with uncaught exception (group=0x414c4700)
08-23 09:14:31.231: E/AndroidRuntime(1109): FATAL EXCEPTION: main
08-23 09:14:31.231: E/AndroidRuntime(1109): java.lang.NumberFormatException: Invalid double: ""
08-23 09:14:31.231: E/AndroidRuntime(1109): at java.lang.StringToReal.invalidReal(StringToReal.java:63)
08-23 09:14:31.231: E/AndroidRuntime(1109): at java.lang.StringToReal.parseDouble(StringToReal.java:248)
08-23 09:14:31.231: E/AndroidRuntime(1109): at java.lang.Double.parseDouble(Double.java:295)
08-23 09:14:31.231: E/AndroidRuntime(1109): at com.firstprogram.MainActivity$2.onClick(MainActivity.java:93)
08-23 09:14:31.231: E/AndroidRuntime(1109): at android.view.View.performClick(View.java:4240)
08-23 09:14:31.231: E/AndroidRuntime(1109): at android.view.View$PerformClick.run(View.java:17721)
08-23 09:14:31.231: E/AndroidRuntime(1109): at android.os.Handler.handleCallback(Handler.java:730)
08-23 09:14:31.231: E/AndroidRuntime(1109): at android.os.Handler.dispatchMessage(Handler.java:92)
08-23 09:14:31.231: E/AndroidRuntime(1109): at android.os.Looper.loop(Looper.java:137)
08-23 09:14:31.231: E/AndroidRuntime(1109): at android.app.ActivityThread.main(ActivityThread.java:5103)
08-23 09:14:31.231: E/AndroidRuntime(1109): at java.lang.reflect.Method.invokeNative(Native Method)
08-23 09:14:31.231: E/AndroidRuntime(1109): at java.lang.reflect.Method.invoke(Method.java:525)
08-23 09:14:31.231: E/AndroidRuntime(1109): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
08-23 09:14:31.231: E/AndroidRuntime(1109): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-23 09:14:31.231: E/AndroidRuntime(1109): at dalvik.system.NativeStart.main(Native Method)
08-23 09:14:31.321: D/dalvikvm(1109): GC_FOR_ALLOC freed 243K, 13% free 2776K/3176K, paused 57ms, total 73ms
08-23 09:14:51.461: D/gralloc_goldfish(1152): Emulator without GPU emulation detected.
08-23 09:21:59.242: I/Choreographer(1152): Skipped 39 frames! The application may be doing too much work on its main thread.
08-23 09:27:10.061: I/Choreographer(1152): Skipped 69 frames! The application may be doing too much work on its main thread.
08-23 09:42:37.441: D/gralloc_goldfish(1211): Emulator without GPU emulation detected.
08-23 09:48:27.291: D/gralloc_goldfish(1258): Emulator without GPU emulation detected.
08-23 09:48:34.741: D/dalvikvm(1258): GC_FOR_ALLOC freed 240K, 13% free 2779K/3176K, paused 68ms, total 76ms
08-23 09:51:57.135: I/Choreographer(1258): Skipped 97 frames! The application may be doing too much work on its main thread.
08-23 09:59:16.411: I/Choreographer(1258): Skipped 38 frames! The application may be doing too much work on its main thread.
08-23 09:59:21.872: D/AndroidRuntime(1258): Shutting down VM
08-23 09:59:21.872: W/dalvikvm(1258): threadid=1: thread exiting with uncaught exception (group=0x414c4700)
08-23 09:59:21.991: E/AndroidRuntime(1258): FATAL EXCEPTION: main
08-23 09:59:21.991: E/AndroidRuntime(1258): java.lang.NumberFormatException: Invalid double: ""
08-23 09:59:21.991: E/AndroidRuntime(1258): at java.lang.StringToReal.invalidReal(StringToReal.java:63)
08-23 09:59:21.991: E/AndroidRuntime(1258): at java.lang.StringToReal.parseDouble(StringToReal.java:248)
08-23 09:59:21.991: E/AndroidRuntime(1258): at java.lang.Double.parseDouble(Double.java:295)
08-23 09:59:21.991: E/AndroidRuntime(1258): at com.firstprogram.MainActivity$1.onClick(MainActivity.java:91)
08-23 09:59:21.991: E/AndroidRuntime(1258): at android.view.View.performClick(View.java:4240)
08-23 09:59:21.991: E/AndroidRuntime(1258): at android.view.View$PerformClick.run(View.java:17721)
08-23 09:59:21.991: E/AndroidRuntime(1258): at android.os.Handler.handleCallback(Handler.java:730)
08-23 09:59:21.991: E/AndroidRuntime(1258): at android.os.Handler.dispatchMessage(Handler.java:92)
08-23 09:59:21.991: E/AndroidRuntime(1258): at android.os.Looper.loop(Looper.java:137)
08-23 09:59:21.991: E/AndroidRuntime(1258): at android.app.ActivityThread.main(ActivityThread.java:5103)
08-23 09:59:21.991: E/AndroidRuntime(1258): at java.lang.reflect.Method.invokeNative(Native Method)
08-23 09:59:21.991: E/AndroidRuntime(1258): at java.lang.reflect.Method.invoke(Method.java:525)
08-23 09:59:21.991: E/AndroidRuntime(1258): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
08-23 09:59:21.991: E/AndroidRuntime(1258): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-23 09:59:21.991: E/AndroidRuntime(1258): at dalvik.system.NativeStart.main(Native Method)
08-23 09:59:25.653: I/Process(1258): Sending signal. PID: 1258 SIG: 9
08-23 09:59:51.091: D/gralloc_goldfish(1314): Emulator without GPU emulation detected.
08-23 09:59:57.921: D/AndroidRuntime(1314): Shutting down VM
08-23 09:59:57.921: W/dalvikvm(1314): threadid=1: thread exiting with uncaught exception (group=0x414c4700)
08-23 09:59:58.061: D/dalvikvm(1314): GC_FOR_ALLOC freed 241K, 13% free 2778K/3176K, paused 62ms, total 70ms
08-23 09:59:58.072: E/AndroidRuntime(1314): FATAL EXCEPTION: main
08-23 09:59:58.072: E/AndroidRuntime(1314): java.lang.NumberFormatException: Invalid double: ""
08-23 09:59:58.072: E/AndroidRuntime(1314): at java.lang.StringToReal.invalidReal(StringToReal.java:63)
08-23 09:59:58.072: E/AndroidRuntime(1314): at java.lang.StringToReal.parseDouble(StringToReal.java:248)
08-23 09:59:58.072: E/AndroidRuntime(1314): at java.lang.Double.parseDouble(Double.java:295)
08-23 09:59:58.072: E/AndroidRuntime(1314): at com.firstprogram.MainActivity$1.onClick(MainActivity.java:92)
08-23 09:59:58.072: E/AndroidRuntime(1314): at android.view.View.performClick(View.java:4240)
08-23 09:59:58.072: E/AndroidRuntime(1314): at android.view.View$PerformClick.run(View.java:17721)
08-23 09:59:58.072: E/AndroidRuntime(1314): at android.os.Handler.handleCallback(Handler.java:730)
08-23 09:59:58.072: E/AndroidRuntime(1314): at android.os.Handler.dispatchMessage(Handler.java:92)
08-23 09:59:58.072: E/AndroidRuntime(1314): at android.os.Looper.loop(Looper.java:137)
08-23 09:59:58.072: E/AndroidRuntime(1314): at android.app.ActivityThread.main(ActivityThread.java:5103)
08-23 09:59:58.072: E/AndroidRuntime(1314): at java.lang.reflect.Method.invokeNative(Native Method)
08-23 09:59:58.072: E/AndroidRuntime(1314): at java.lang.reflect.Method.invoke(Method.java:525)
08-23 09:59:58.072: E/AndroidRuntime(1314): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
08-23 09:59:58.072: E/AndroidRuntime(1314): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-23 09:59:58.072: E/AndroidRuntime(1314): at dalvik.system.NativeStart.main(Native Method)
08-23 10:00:00.761: I/Process(1314): Sending signal. PID: 1314 SIG: 9
08-23 10:01:31.132: D/gralloc_goldfish(1365): Emulator without GPU emulation detected.
08-23 10:01:47.221: D/dalvikvm(1365): GC_FOR_ALLOC freed 242K, 13% free 2777K/3176K, paused 77ms, total 85ms
08-23 10:01:47.221: D/AndroidRuntime(1365): Shutting down VM
08-23 10:01:47.232: W/dalvikvm(1365): threadid=1: thread exiting with uncaught exception (group=0x414c4700)
08-23 10:01:47.281: E/AndroidRuntime(1365): FATAL EXCEPTION: main
08-23 10:01:47.281: E/AndroidRuntime(1365): java.lang.NumberFormatException: Invalid double: ""
08-23 10:01:47.281: E/AndroidRuntime(1365): at java.lang.StringToReal.invalidReal(StringToReal.java:63)
08-23 10:01:47.281: E/AndroidRuntime(1365): at java.lang.StringToReal.parseDouble(StringToReal.java:248)
08-23 10:01:47.281: E/AndroidRuntime(1365): at java.lang.Double.parseDouble(Double.java:295)
08-23 10:01:47.281: E/AndroidRuntime(1365): at com.firstprogram.MainActivity$1.onClick(MainActivity.java:92)
08-23 10:01:47.281: E/AndroidRuntime(1365): at android.view.View.performClick(View.java:4240)
08-23 10:01:47.281: E/AndroidRuntime(1365): at android.view.View$PerformClick.run(View.java:17721)
08-23 10:01:47.281: E/AndroidRuntime(1365): at android.os.Handler.handleCallback(Handler.java:730)
08-23 10:01:47.281: E/AndroidRuntime(1365): at android.os.Handler.dispatchMessage(Handler.java:92)
08-23 10:01:47.281: E/AndroidRuntime(1365): at android.os.Looper.loop(Looper.java:137)
08-23 10:01:47.281: E/AndroidRuntime(1365): at android.app.ActivityThread.main(ActivityThread.java:5103)
08-23 10:01:47.281: E/AndroidRuntime(1365): at java.lang.reflect.Method.invokeNative(Native Method)
08-23 10:01:47.281: E/AndroidRuntime(1365): at java.lang.reflect.Method.invoke(Method.java:525)
08-23 10:01:47.281: E/AndroidRuntime(1365): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
08-23 10:01:47.281: E/AndroidRuntime(1365): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-23 10:01:47.281: E/AndroidRuntime(1365): at dalvik.system.NativeStart.main(Native Method)
08-23 10:01:49.873: I/Process(1365): Sending signal. PID: 1365 SIG: 9