我开发了一个简单的测验应用程序,但我认为代码有问题。如果文本字段留空,当我单击提交按钮时它会崩溃。这是我的代码。
public class Quiz extends Activity
{
Button submit;
EditText e1,e2,e3,e4,e5;
int ctr;
String msg = "", msg1 = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quiz);
submit=(Button)findViewById(R.id.button1);
e1=(EditText)findViewById(R.id.editText1);
e2=(EditText)findViewById(R.id.editText2);
e3=(EditText)findViewById(R.id.editText3);
e4=(EditText)findViewById(R.id.editText4);
e5=(EditText)findViewById(R.id.editText5);
submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0){
String ans1 = String.valueOf(e1.getText());
int ans11 = Integer.parseInt(ans1);
String ans2 = String.valueOf(e2.getText());
int ans22 = Integer.parseInt(ans2);
String ans3 = String.valueOf(e3.getText());
int ans33 = Integer.parseInt(ans3);
String ans4 = String.valueOf(e4.getText());
int ans44 = Integer.parseInt(ans4);
String ans5 = String.valueOf(e5.getText());
int ans55 = Integer.parseInt(ans5);
if(e1.getText().toString().equals("")||e2.getText().toString().equals("")||e3.getText().toString().equals("")||e4.getText().toString().equals("")||e5.getText().toString().equals("")){
msg1 = "Text fields cannot be empty.";
Toast.makeText(Quiz.this, msg1, Toast.LENGTH_LONG).show();
}
else{
if(ans11==4){
ctr++;
}
if(ans22==3){
ctr++;
}
if(ans33==1){
ctr++;
}
if(ans44==2){
ctr++;
}
if(ans55==5){
ctr++;
}
msg = "Your score is " + ctr;
Toast.makeText(Quiz.this, msg, Toast.LENGTH_LONG).show();
}
}
});
}
日志猫说:
10-06 21:47:35.888: E/AndroidRuntime(1759): FATAL EXCEPTION: main
10-06 21:47:35.888: E/AndroidRuntime(1759): java.lang.NumberFormatException: unable to parse '' as integer
10-06 21:47:35.888: E/AndroidRuntime(1759): at java.lang.Integer.parseInt(Integer.java:362)
10-06 21:47:35.888: E/AndroidRuntime(1759): at java.lang.Integer.parseInt(Integer.java:332)
10-06 21:47:35.888: E/AndroidRuntime(1759): at com.example.capslock.Quiz$1.onClick(Quiz.java:39)
10-06 21:47:35.888: E/AndroidRuntime(1759): at android.view.View.performClick(View.java:2485)
10-06 21:47:35.888: E/AndroidRuntime(1759): at android.view.View$PerformClick.run(View.java:9080)
10-06 21:47:35.888: E/AndroidRuntime(1759): at android.os.Handler.handleCallback(Handler.java:587)
10-06 21:47:35.888: E/AndroidRuntime(1759): at android.os.Handler.dispatchMessage(Handler.java:92)
10-06 21:47:35.888: E/AndroidRuntime(1759): at android.os.Looper.loop(Looper.java:130)
10-06 21:47:35.888: E/AndroidRuntime(1759): at android.app.ActivityThread.main(ActivityThread.java:3683)
10-06 21:47:35.888: E/AndroidRuntime(1759): at java.lang.reflect.Method.invokeNative(Native Method)
10-06 21:47:35.888: E/AndroidRuntime(1759): at java.lang.reflect.Method.invoke(Method.java:507)
10-06 21:47:35.888: E/AndroidRuntime(1759): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
10-06 21:47:35.888: E/AndroidRuntime(1759): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
10-06 21:47:35.888: E/AndroidRuntime(1759): at dalvik.system.NativeStart.main(Native Method)