简体   繁体   中英

Android - Checkbox/radio button score quiz app

I would like to ask for some help with my android code.

It should add one point with correct radiobutton and checkbox click - which it does - but there is an error, which I can't find out why.:

here is the java code:

package com.example.android.myquizapp;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import static com.example.android.myquizapp.R.id.Tom;
import static com.example.android.myquizapp.R.id.score;


public class MainActivity extends AppCompatActivity {
    Button btnSubmit;
    int score = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

       Button btnSubmit = (Button)findViewById(R.id.btnSubmit);
        btnSubmit.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View view){
                int score = 0;
                if (((RadioButton)findViewById(R.id.Monica)).isChecked()) {score++;}
                if (((RadioButton)findViewById(R.id.h5)).isChecked()) {score++;}
                if (((RadioButton)findViewById(R.id.a3)).isChecked()) {score++;}            
                if (((CheckBox) findViewById(R.id.Tom)).isChecked()){score++;}
                if (((CheckBox) findViewById(R.id.Brad)).isChecked()){score++;}
                if (((CheckBox) findViewById(R.id.Bruce)).isChecked()){score++;}

            {displayResult(score);}}

    });}
    private void displayResult() {
        String message = "You scored " + score;
        message += " out of 6";
        message += "\nWell done!";
        Toast toast = Toast.makeText(this, message, Toast.LENGTH_SHORT);
        toast.setGravity(Gravity.CENTER, 0 , 0);
        toast.show();
    }}

the word "score" is red underlined in Android studio:

{displayResult(score);}}

the app works fine, so not quite understand why.

Can you help me?

Your displayResult() method does not have any arguments. And you are passing an argument to it. That is why your are seeing that red line

And How on the earth is your app working fine. You can't even compile that code with this error.

firstly you have declare int score = 0; globally, So you don't need to declare locally in Onclick method. remove following line from Onclick method.

int score = 0;

then you are passing param score to displayResult() method but you have declared with no params.call

   displayResult();

Firstly you are passing the int score to the displayResult() method which is not expecting any arguments and secondly you are wrapping it around extra {} brackets which is wrong.

Also you have initialized Button btnSubmit twice which is also wrong and now since you are passing the score value there is no need to create global score which is redundant and not being used anywhere.

You should modify you code as:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button btnSubmit = (Button) findViewById(R.id.btnSubmit);
        btnSubmit.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View view){
                int score = 0;
                if (((RadioButton)findViewById(R.id.Monica)).isChecked()) {score++;}
                if (((RadioButton)findViewById(R.id.h5)).isChecked()) {score++;}
                if (((RadioButton)findViewById(R.id.a3)).isChecked()) {score++;}
                if (((CheckBox) findViewById(R.id.Tom)).isChecked()){score++;}
                if (((CheckBox) findViewById(R.id.Brad)).isChecked()){score++;}
                if (((CheckBox) findViewById(R.id.Bruce)).isChecked()){score++;}

                displayResult(score);
            }

        });}
    private void displayResult(int score) {
        String message = "You scored " + score;
        message += " out of 6";
        message += "\nWell done!";
        Toast toast = Toast.makeText(this, message, Toast.LENGTH_SHORT);
        toast.setGravity(Gravity.CENTER, 0 , 0);
        toast.show();
    }
}

Hope this solves your issues

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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