【发布时间】:2011-12-19 12:01:02
【问题描述】:
我正在尝试构建这个测验程序,其中一个人被问到一个问题,然后必须回答,这是一个两个人的游戏,他们只需完成所有问题,游戏就结束了。
到目前为止,我已经设法将数组(strings.xml 文件中的数组)加载到 arraylist 中,然后从 0 到 arraylist.size 获取随机 id,然后显示该字符串(问题)然后它会删除随机 id 并取一个新的。
但是当我遍历数组列表中的所有 id 时程序崩溃,我的代码如下所示:http://pastebin.com/MaEj49BL
重要的是:
public void gameCode()
{
setContentView(R.layout.game);
if (mList == null)
{
String[] mQuestions = getResources().getStringArray(R.array.mQuestions);
mList = new ArrayList();
Collections.addAll(mList, mQuestions);
}
else if (mList.isEmpty())
{
m = (TextView)findViewById(R.id.textView1);
m.setText("You've run out of questions!");
}
if (wList == null)
{
String[] wQuestions = getResources().getStringArray(R.array.wQuestions);
wList = new ArrayList();
Collections.addAll(wList, wQuestions);
}
else if (wList.isEmpty())
{
w = (TextView)findViewById(R.id.textView1);
w.setText("You've run out of questions!");
}
//Takes a random ID from the arraylist
int rndm = generator.nextInt(mList.size());
int rndw = generator.nextInt(wList.size());
//Sets the Strings to a random number from one of the array lists
String qMan = (String) mList.get(rndm);
String qWoman = (String) wList.get(rndw);
if(i == 0)
{
//Defines that w is textView1 with the value of qWoman
w = (TextView)findViewById(R.id.textView1);
w.setText(qWoman);
wList.remove(rndw);
i = 1;
final Button buttonNext = (Button) findViewById(R.id.buttonNext);
buttonNext.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
gameCode();
}
});
}
else
{
m = (TextView)findViewById(R.id.textView1);
m.setText(qMan);
mList.remove(rndm);
i = 0;
final Button buttonNext = (Button) findViewById(R.id.buttonNext);
buttonNext.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
gameCode();
}
});
}
}
}
来自 pastebin 的源代码中有 cmets。现在我只需要将文本更改为“您的问题已用完”
【问题讨论】:
标签: java android arrays arraylist