无法为最终变量赋值 - Android

时间:2014-06-23 12:41:15

标签: java android textview

我正在尝试定义一个TextView final但是说:无法为最终变量赋值。

在下面的代码中,我插入了一些注释,以帮助解决问题。

有任何帮助吗?

提前致谢!

CODE:

public class single_presentation extends Activity
{
    List<Slider> list;
    final TextView correctAnswerHolder = null;
    int i = 0;

    DatabaseHelper db = new DatabaseHelper(this);

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

        String value = getIntent().getStringExtra("PRESENTATION_SESSION_ID");

        list = db.getAllSlidersByPresentation(value);
        db.close();

        String question = list.get(0).getQuestion();
        TextView questionText = (TextView) findViewById(R.id.questionText);
        questionText.setText(question);

        String correctAnswer = list.get(0).getCorrectAnswer();
        TextView correctAnswerText = (TextView) findViewById(R.id.correctAnswerText);
        String wrongAnswer_1 = list.get(0).getWrongAnswer1();
        TextView wrongAnswerText_1 = (TextView) findViewById(R.id.wrongAnswerText_1);
        String wrongAnswer_2 = list.get(0).getWrongAnswer2();
        TextView wrongAnswerText_2 = (TextView) findViewById(R.id.wrongAnswerText_2);
        String wrongAnswer_3 = list.get(0).getWrongAnswer3();
        TextView wrongAnswerText_3 = (TextView) findViewById(R.id.wrongAnswerText_3);

        Uri imgUri = list.get(i).getImageURI();
        ImageView imageView = (ImageView)findViewById(R.id.imageView);
        imageView.setImageURI(imgUri);

        final String [] answers = {correctAnswer, wrongAnswer_1, wrongAnswer_2, wrongAnswer_3};

        ArrayList<String> answersList = new ArrayList<String>(Arrays.asList(answers));
        long seed = System.nanoTime();
        Collections.shuffle(answersList, new Random(seed));

        Log.e("First Slider Answers ", answersList.toString());

        correctAnswerText.setText(answersList.get(0));
        wrongAnswerText_1.setText(answersList.get(1));
        wrongAnswerText_2.setText(answersList.get(2));
        wrongAnswerText_3.setText(answersList.get(3));

        if (correctAnswerText.getText().toString().equals(correctAnswer))
            correctAnswerHolder = correctAnswerText; // HERE is the problem
        else if (wrongAnswerText_1.getText().toString().equals(correctAnswer))
            correctAnswerHolder = wrongAnswerText_1; // HERE is the problem
        else if (wrongAnswerText_2.getText().toString().equals(correctAnswer))
            correctAnswerHolder = wrongAnswerText_2; // HERE is the problem
        else if (wrongAnswerText_3.getText().toString().equals(correctAnswer))
            correctAnswerHolder = wrongAnswerText_3; // HERE is the problem

2 个答案:

答案 0 :(得分:0)

如果声明变量final,则只能分配一次。

更多信息: http://www.javamex.com/tutorials/synchronization_final.shtml

答案 1 :(得分:0)

在java中声明为final的任何变量都不能重新赋值。在这里,您首先为变量赋值null,这样您就无法更改它。 你能详细说明为什么你宣布你的变量是最终的吗?