2014-06-23 206 views
0

我想定義一個TextView最終變量但是說:不能賦值給最終變量。不能賦值給最終變量 - Android

在下面的代碼中,我插入了一些評論,以幫助捕捉更容易的問題。

任何幫助?

在此先感謝!

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 
+0

是的,你不能,爲什麼你要聲明,作爲'最後'? –

+0

你真的知道'Java'嗎?你需要閱讀Java編程書! –

+0

我想在correctAnswerHolder.setOnClickListener中使用它以獲取correctAnswerHolder的更改。 – Joan

回答

0

如果要聲明一個變量最終只能被分配一次。

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

+0

謝謝@Deacovy,我已經讀過某處宣佈textview最終以解決我的問題.. 請看看這個:http://stackoverflow.com/questions/24361042/how-to-make- setonclicklistener-capable-of-updating-value – Joan

0

任何變量聲明爲final在java中不能被重新分配的值。在這裏,您首先將null分配給變量,以便您不能更改它。 你能否詳細說明爲什麼你宣佈你的變量最終?

+0

這不是一個答案..反正它應該是一個評論。 –