【问题标题】:Android/Java: Check if arraylist is empty and then launch another activityAndroid/Java:检查 arraylist 是否为空,然后启动另一个活动
【发布时间】: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


    【解决方案1】:

    使用 null 安全的 CollectionUtils.isEmpty()

    【讨论】:

    • 我应该在 else if 语句中使用这个吗?
    【解决方案2】:

    您正在检查 2 个数组是否为空,但当它们为空时,您并没有短路逻辑的其余部分......

    如果 mList 和 wList 为空,您应该绕过该方法的其余部分,或者至少在尝试从每个数组中获取下一个随机问题之前检查 mList.size() > 0 and wList.size()>0

    【讨论】:

    • 程序运行得很好,崩溃的地方是当我用完arraylist中的项目时。
    • 对。因此,当您在数组列表中没有更多项目时,您仍在执行尝试随机选择问题并将问题文本设置到视图中的代码。举个例子:假设您在 wList 中有 0 个项目并且 i==0,那么随机数生成器将返回 0,而您执行 wList.get(rdmn) 的尝试将会失败。
    • 谢谢.. 我在随机生成器等之前添加了一个 if 语句,它的工作原理就像一个 charmcode if (mList.size() > 0 && wList.size() > 0)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-18
    • 1970-01-01
    相关资源
    最近更新 更多