Android radioButton错误

时间:2014-07-06 15:13:50

标签: android radiobuttonlist

我在这一行收到错误,我不知道为什么。

c1.setText(answers[currentAnswer]);

c1 c2c3RadioButton列表

的一部分

答案是字符串列表 -

answers = new String[]{"Cairo","ac"};

这是我正在使用它的功能

public void showQuestion()
    {
    currentQuestion++;
    currentAnswer=currentAnswer++;//plus 3
    if(currentQuestion == questions.length)
    {
    currentQuestion =0;
    currentAnswer =0;
    }
    questionView.setText(questions[currentQuestion]);
    c1.setText(answers[currentAnswer]);
    //c2.setText(answers[currentAnswer+1]);
    //c3.setText(answers[currentAnswer+2]);
    answerView.setText("");
    answerText.setText("");
    }

这是整个代码

       package com.example.testeautov2;
       import android.app.Activity;
       import android.os.Bundle;
       import android.view.Menu;
       import android.view.MenuItem;
       import android.view.View;
       import android.view.View.OnClickListener;
       import android.widget.Button;
       import android.widget.EditText;
       import android.widget.RadioButton;
       import android.widget.TextView;

          public class MainActivity extends Activity {
private int currentQuestion;
private int currentAnswer;
private String[] questions;
private String[] answers;
private String[] raspunsuri;
private Button answerButton;
private Button questionButton;
private TextView questionView;
private TextView answerView;
private EditText answerText;
private RadioButton c1;
private RadioButton c2;
private RadioButton c3;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    init();
}
    public void init()
    {
    questions = new String[]{"What is the capital of Egypt?",
    "What class are you in right now?"};
    answers = new String[]{"Cairo","ac"};
    raspunsuri=new String[]{"Cairo","gresit","gresit","test","ac","test"};
    currentQuestion =-1;
    currentAnswer=-1;
    //sau -1 ca sa fie gol la inceput
    answerButton = (Button)findViewById(R.id.AnswerButton);

     c1 = (RadioButton)findViewById(R.id.Raspuns1Radio);
     c2 = (RadioButton)findViewById(R.id.Raspuns2Radio);
     c3 = (RadioButton)findViewById(R.id.Raspuns3Radio);



    //submitu
    questionButton = (Button)findViewById(R.id.QuestionButton);
    //next question
    questionView = (TextView)findViewById(R.id.QuestionTextView);
    answerView = (TextView) findViewById(R.id.AnswerTextView);
    answerText = (EditText) findViewById(R.id.AnswerText);
    answerButton.setOnClickListener(new OnClickListener(){
    public void onClick(View v) {
    checkAnswer();
    }});
    questionButton.setOnClickListener(new OnClickListener(){
    public void onClick(View v) {
    showQuestion();
    }});
    }
    public void showQuestion()
    {
    currentQuestion++;
    currentAnswer=currentAnswer++;//plus 3
    if(currentQuestion == questions.length)
    {
    currentQuestion =0;
    currentAnswer =0;
    }
    questionView.setText(questions[currentQuestion]);
    c1.setText(answers[currentAnswer]);
    //c2.setText(answers[currentAnswer+1]);
    //c3.setText(answers[currentAnswer+2]);
    answerView.setText("");
    answerText.setText("");
    }
    /*
    * This method return true if the answer equals to correct
    answer
    * (Ignoring case)
    */
    public boolean isCorrect(String answer)
    {
    return (answer.equalsIgnoreCase(answers[currentQuestion]));
    }
    /* this method :
    * 1: Read the text ( answer) from the answerTextEdit
    * 2: call the isCorrect method
    * 3: display the appropriate message.
    */
    public void checkAnswer()
    {
        //asta e pentru textfield
//  String answer = answerText.getText().toString();
        //pentru radio
        String answer = getSelectedAnswer();


    if(isCorrect(answer))
    answerView.setText("You're right!");
    else
    answerView.setText("Sorry, the correct answer is"+answers[currentQuestion]);
    }

    private String getSelectedAnswer() {

        if (c1.isChecked())
        {
        return c1.getText().toString();
        }
        if (c2.isChecked())
        {
        return c2.getText().toString();
        }
        if (c3.isChecked())
        {
        return c3.getText().toString();
        }


        return null;
        }

}

0 个答案:

没有答案