Android Quiz应用在更新分数时崩溃

时间:2017-06-05 17:43:44

标签: java android

提交按钮ID为ShowScore 正确答案是right1,right2 ... right53 单击按钮时,应用程序崩溃... android studios doest显示任何错误 正确答案奖励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);
    }
}

2 个答案:

答案 0 :(得分:1)

#。我猜你已经使用属性setScore()在XML中定义了Button方法来提交android:onClick="setScore"。但是您的方法没有任何View参数。更新您的方法如下:

    public void setScore(View v) {
        ..........
        .............
    }

#。由于scoreint值,请使用olds.setText(String.valueOf(score))score上设置TextView

以下是完整代码:

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

答案 1 :(得分:0)

setText.(score)更改为(score+"");

说明:setText方法只接受String作为参数但是你可以传入一个int,但它会产生错误,我想android平台中的输出流必须始终是String类型。

你甚至可以使用String.valueOf(score);,因为这是正确的方法,并且在第一个参数中设置的引号就像极客作弊。如果您在xml中设置了onClick方法,那么setScore()必须使用参数setScore (View view) { //text view goes here..}

希望它有效。干杯!