gpt4 book ai didi

java - Android 测验应用程序崩溃

转载 作者:行者123 更新时间:2023-12-01 14:02:48 25 4
gpt4 key购买 nike

我正在开发一个简单的测验应用程序,但我认为代码有问题。如果文本字段留空,当我单击提交按钮时,它会崩溃。这是我的代码。

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();
}


}
});
}

logcat 说:

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)

最佳答案

尝试使用

从 editText 获取字符串
editText.getText().toString();

如果你想将一个字符串与另一个字符串进行比较,请执行以下操作:

if(ans1.equals("4")){...}

如果你想从 EditText 得到一个整数,试试这个

int a11 = 0;
try {
a11 = Integer.parseInt(editText.getText().toString());
}catch(NumberFormatException e){ }

因此,当您的 EditText 中没有字符串或垃圾字符串时,您不会收到异常

我是对的,你试图从“”解析一个整数:-D

关于java - Android 测验应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19211208/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com