mardi 23 mai 2017

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?




Aucun commentaire:

Enregistrer un commentaire