简体   繁体   English

更新分数时,Android Quiz应用程序崩溃

[英]Android Quiz app crashing while updating score

The submit button id is ShowScore correct answers are right1,right2...right53 when the button is clicked the app crashes... android studios doest show any errors right answer awards 1 point wrong answer is -1 please help me out 提交按钮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);
    }
}

#. I guess you have defined setScore() method to SUBMIT Button in your XML using attribute android:onClick="setScore" . 我想您已经使用属性android:onClick="setScore"在XML中定义了setScore()方法来提交Button But your method doesn't have any View parameter. 但是您的方法没有任何View参数。 Update your method as below: 如下更新您的方法:

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

#. As score is an int value, use olds.setText(String.valueOf(score)) to set score on TextView . 由于score是一个int值,请使用olds.setText(String.valueOf(score))TextView上设置score

Here is the full code: 这是完整的代码:

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

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

Explanation: setText method accepts only String as an argument but yes you can pass in an int but it will give an error and I suppose that output stream in android platform must always be String type. 说明:setText方法仅接受String作为参数,但是是的,您可以传入int,但会给出错误,并且我认为android平台中的输出流必须始终为String类型。

You can even use String.valueOf(score); 您甚至可以使用String.valueOf(score); since this is the right way and adding quotation marks set in the first paramater is like a geek cheat. 因为这是正确的方法,所以在第一个参数中添加引号设置就像是极客作弊。 If you have set onClick method in xml, then setScore() must take the parameter setScore (View view) { //text view goes here..} 如果您已在xml中设置onClick方法,则setScore()必须采用参数setScore (View view) { //text view goes here..}

Hope it works. 希望它能工作。 Cheers! 干杯!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM