android用if判断对象是否为空,Android / Java:检查arraylist是否为空,然后启动...

我试图建立这个测验计划,其中一个人被问到一个问题,然后必须回答,这是一个双人游戏,他们只是通过所有问题,游戏结束.

到目前为止我设法做的是将数组(strings.xml文件中的数组)加载到arraylist然后从0到arraylist.size的随机id然后显示该字符串(问题)的通用代码然后它删除随机ID并采用一个新的.

但是当我运行arraylist中的所有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的源代码中有注释.现在我只需要将文本更改为“你已经用完了问题”

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值