2013-03-01 62 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 ++)语句需要执行一次。现在它只执行一次,设置新的问题和图像按钮,但你的应用程序不知道从那里去哪里。