java - 安卓/Java : Check if arraylist is empty and then launch another activity

标签 java android arrays arraylist

我正在尝试构建这个测验程序,在这个程序中,一个人被问到一个问题,然后必须回答,这是一个两个人的游戏,他们只需完成所有问题,然后游戏结束。

到目前为止,我所做的是将数组(strings.xml 文件中的数组)加载到数组列表中的通用代码,然后取一个从 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 的源代码中有注释。现在我只需要将文本更改为“你的问题已经用完了”

最佳答案

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

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

关于java - 安卓/Java : Check if arraylist is empty and then launch another activity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7966991/

相关文章:

java - 在 Clojure 中调用不带参数的 Java 实例方法

java - 获取数字字符串的一部分

java - 在Java中向同一进程输入不同的字符串

android - 单击通知时启动自定义对话框

android - 使用 match_parent 在 ListView 中覆盖 RelativeLayout

java - 如何在 Java 中与智能卡交互?

Android & Servers - 可以用什么服务器软件,一头雾水

c++ - 用于队列模拟的数组与双向链表

javascript - javascript "break"会完全停止递归函数还是只是停止该实例?

arrays - 如何在 mongoDB 中提取嵌入式文档中的特定元素