2013-03-01 40 views
1

獲得資源的Android - 從XMLAndroid的 - 無法從XML

嗨獲得資源,

我開發一個問答遊戲的Android應用程序,遊戲中有24個問題,每個問題有一個String (問題)和四ImageButton (answer), when user select the correct ImageButton`,然後他們去下一個問題。

但問題是,在選擇第一個正確答案之後,屏幕會刷新一個新問題和一組新圖像,但會凍結在那裏,無論點擊什麼,它都不會進入下一個問題。我附上我的代碼和任何幫助讚賞!

public void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.activity_new_game); 

// set variables 
int fourImageId[] = { R.id.1_4a, R.id.1_4b,R.id.1_4c, R.id.1_4d }; 
questionText = (TextView) findViewById(R.id.question1); 
questionList = getResources().getStringArray(R.array.question); 
correctAnswerIdList = ListOfImages.getListOfCorrectImages(); 
imageIdList = ListOfImages.getListOfImages(); //two dimension array 

// give values to four image buttons 
for (int i = 0; i < 4; i++) { 
    fourImages[i] = (ImageButton) findViewById(fourImageId[i]); 
    final int temp = fourImageId[i]; 
    fourImages[i].setOnClickListener(new View.OnClickListener() { 
     @Override    
     public void onClick(View v) { 
      if (temp==correctAnswerIdList[index]){ 
       questionText.setText(questionList[index]); 
       for (int i = 0; i < imageIdList[index].length; i++) { 
        fourImages[i].setImageResource(imageIdList[index][i]);} 
      index++; 
     } 
    } 
});} 
+0

logcat中有什麼有用的東西嗎? – 2013-03-01 15:07:39

回答

1

在所有按鈕點擊偵聽器設置完成後,您的臨時變量永遠不會改變。因此,if (temp==correctAnswerIdList[index])語句只能成功執行一次。

+0

嗨,感謝您的回覆,我現在可以瞭解問題出在哪裏,我應該把溫度移到別的地方嗎? – Teresa 2013-03-01 16:08:37

+0

temp可能需要更新到'onClick'方法的'if'塊內的'fourImageId'的不同索引。如果'questionList []'和'fourImageId []'索引是對齊的,你可以在設置'questionText'後直接添加一行:'temp = fourImageId [index]'。 – Shellum 2013-03-01 16:24:34

0

對於每個新問題,Your for(int i = 0; i < 4; i ++)語句需要執行一次。現在它只執行一次,設置新的問題和圖像按鈕,但你的應用程序不知道從那裏去哪裏。