[英]Android Quiz app crashing while updating score
提交按鈕ID是ShowScore正確答案是right1,right2 ... right53,當單擊該應用程序時崩潰... android studios不顯示任何錯誤正確答案獎勵1分錯誤答案是-1請幫幫我
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.CheckBox;
import android.widget.RadioButton;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
int score = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void setScore() {
RadioButton check1 = (RadioButton) findViewById(R.id.right1);
boolean doit1 = check1.isChecked();
RadioButton check2 = (RadioButton) findViewById(R.id.right2);
boolean doit2 = check2.isChecked();
RadioButton check3 = (RadioButton) findViewById(R.id.right3);
boolean doit3 = check3.isChecked();
RadioButton check4 = (RadioButton) findViewById(R.id.right4);
boolean doit4 = check4.isChecked();
CheckBox check5 = (CheckBox) findViewById(R.id.right51);
boolean doit5 = check5.isChecked();
CheckBox check52 = (CheckBox) findViewById(R.id.right52);
boolean doit52 = check52.isChecked();
CheckBox check53 = (CheckBox) findViewById(R.id.right53);
boolean doit53 = check53.isChecked();
updateScore(doit1);
updateScore2(doit2);
updateScore3(doit3);
updateScore4(doit4);
updateScore5(doit5, doit52, doit53);
showScore();
}
private int updateScore(boolean doit1) {
if (doit1) {
score = score + 1;
} else {
score = score - 1;
}
return score;
}
private int updateScore2(boolean doit2) {
if (doit2) {
score = score + 1;
} else {
score = score - 1;
}
return score;
}
private int updateScore3(boolean doit3) {
if (doit3) {
score = score + 1;
} else {
score = score - 1;
}
return score;
}
private int updateScore4(boolean doit4) {
if (doit4) {
score = score + 1;
} else {
score = score - 1;
}
return score;
}
private int updateScore5(boolean doit5, boolean doit52, boolean doit53) {
if (doit5 && doit52 && doit53) {
score = score + 1;
} else {
score = score - 1;
}
return score;
}
private void showScore() {
TextView olds = (TextView) findViewById(R.id.ShowScore);
olds.setText(score);
}
}
# 我想您已經使用屬性android:onClick="setScore"
在XML中定義了setScore()
方法來提交Button
。 但是您的方法沒有任何View
參數。 如下更新您的方法:
public void setScore(View v) {
..........
.............
}
# 由於score
是一個int
值,請使用olds.setText(String.valueOf(score))
在TextView
上設置score
。
這是完整的代碼:
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.CheckBox;
import android.widget.RadioButton;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
TextView olds;
RadioButton check1, check2, check3, check4;
CheckBox check5, check52, check53;
int score = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
olds = (TextView) findViewById(R.id.ShowScore);
check1 = (RadioButton) findViewById(R.id.right1);
check2 = (RadioButton) findViewById(R.id.right2);
check3 = (RadioButton) findViewById(R.id.right3);
check4 = (RadioButton) findViewById(R.id.right4);
check5 = (CheckBox) findViewById(R.id.right51);
check52 = (CheckBox) findViewById(R.id.right52);
check53 = (CheckBox) findViewById(R.id.right53);
}
public void setScore(View v) {
boolean doit1 = check1.isChecked();
boolean doit2 = check2.isChecked();
boolean doit3 = check3.isChecked();
boolean doit4 = check4.isChecked();
boolean doit5 = check5.isChecked();
boolean doit52 = check52.isChecked();
boolean doit53 = check53.isChecked();
updateScore(doit1);
updateScore2(doit2);
updateScore3(doit3);
updateScore4(doit4);
updateScore5(doit5, doit52, doit53);
showScore();
}
private int updateScore(boolean doit1) {
if (doit1) {
score = score + 1;
} else {
score = score - 1;
}
return score;
}
private int updateScore2(boolean doit2) {
if (doit2) {
score = score + 1;
} else {
score = score - 1;
}
return score;
}
private int updateScore3(boolean doit3) {
if (doit3) {
score = score + 1;
} else {
score = score - 1;
}
return score;
}
private int updateScore4(boolean doit4) {
if (doit4) {
score = score + 1;
} else {
score = score - 1;
}
return score;
}
private int updateScore5(boolean doit5, boolean doit52, boolean doit53) {
if (doit5 && doit52 && doit53) {
score = score + 1;
} else {
score = score - 1;
}
return score;
}
private void showScore() {
olds.setText(String.valueOf(score));
}
}
將setText.(score)
更改為(score+"");
說明:setText方法僅接受String作為參數,但是是的,您可以傳入int,但會給出錯誤,並且我認為android平台中的輸出流必須始終為String類型。
您甚至可以使用String.valueOf(score);
因為這是正確的方法,所以在第一個參數中添加引號設置就像是極客作弊。 如果您已在xml中設置onClick方法,則setScore()
必須采用參數setScore (View view) { //text view goes here..}
希望它能工作。 干杯!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.