mardi 28 février 2017

getting error when size of List becomes 0

Kindly look into this code, i am making a quiz which will generate random question but it crashed when size of List becomes zero .Kindly look into this code, i am making a quiz which will generate random question but it crashed when size of List becomes zero .Kindly look into this code, i am making a quiz which will generate random question but it crashed when size of List becomes zero .

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_questions);
    textView = (TextView) findViewById(R.id.textView);
    tick = (ImageView) findViewById(R.id.tick);
    cross = (ImageView) findViewById(R.id.cross);
    Qlist = new ArrayList<>();
    q1 = (LinearLayout) findViewById(R.id.q1);
    q2 = (LinearLayout) findViewById(R.id.q2);
    q3 = (LinearLayout) findViewById(R.id.q3);
    q4 = (LinearLayout) findViewById(R.id.q4);
    q5 = (LinearLayout) findViewById(R.id.q5);
    q6 = (LinearLayout) findViewById(R.id.q6);
    q7 = (LinearLayout) findViewById(R.id.q7);
    q8 = (LinearLayout) findViewById(R.id.q8);
    q9 = (LinearLayout) findViewById(R.id.q9);
    q10 = (LinearLayout) findViewById(R.id.q10);
    q11 = (LinearLayout) findViewById(R.id.q11);
    q12 = (LinearLayout) findViewById(R.id.q12);

    qA1 = (TextView) findViewById(R.id.q1optA);
    qA2 = (TextView) findViewById(R.id.q2optA);
    qA3 = (TextView) findViewById(R.id.q3optA);
    qA4 = (TextView) findViewById(R.id.q4optA);
    qA5 = (TextView) findViewById(R.id.q5optA);
    qA6 = (TextView) findViewById(R.id.q6optA);
    qA7 = (TextView) findViewById(R.id.q7optA);
    qA8 = (TextView) findViewById(R.id.q8optA);
    qA9 = (TextView) findViewById(R.id.q9optA);
    qA10 = (TextView) findViewById(R.id.q10optA);
    qA11 = (TextView) findViewById(R.id.q11optA);
    qA12 = (TextView) findViewById(R.id.q12optA);

    qB1 = (TextView) findViewById(R.id.q1optB);
    qB2 = (TextView) findViewById(R.id.q2optB);
    qB3 = (TextView) findViewById(R.id.q3optB);
    qB4 = (TextView) findViewById(R.id.q4optB);
    qB5 = (TextView) findViewById(R.id.q5optB);
    qB6 = (TextView) findViewById(R.id.q6optB);
    qB7 = (TextView) findViewById(R.id.q7optB);
    qB8 = (TextView) findViewById(R.id.q8optB);
    qB9 = (TextView) findViewById(R.id.q9optB);
    qB10 = (TextView) findViewById(R.id.q10optB);
    qB11 = (TextView) findViewById(R.id.q11optB);
    qB12 = (TextView) findViewById(R.id.q12optB);

    quest = getResources().getStringArray(R.array.questions);
    Collections.addAll(Qlist, quest);
    randomStr = (String) Qlist.get(new Random().nextInt(Qlist.size() - 1));
    textView.setText(randomStr);
    if (randomStr == Qlist.get(0)) {
        Question1();
    }
    if (randomStr == Qlist.get(1)) {
        Question2();

    }
    if (randomStr == Qlist.get(2)) {
        Question3();

    }
    if (randomStr == Qlist.get(3)) {
        Question4();
    }
    if (randomStr == Qlist.get(4)) {
        Question5();
    }
    if (randomStr == Qlist.get(5)) {
        Question6();

    }
    if (randomStr == Qlist.get(6)) {
        Question7();

    }
    if (randomStr == Qlist.get(7)) {
        Question8();

    }
    if (randomStr == Qlist.get(8)) {
        Question9();

    }
    if (randomStr == Qlist.get(9)) {
        Question10();

    }
    if (randomStr == Qlist.get(10)) {
        Question11();

    }
    if (randomStr == Qlist.get(11)) {
        Question12();

    }
}

public void Question1() {

    q1.setVisibility(View.VISIBLE);
    q3.setVisibility(View.INVISIBLE);
    q4.setVisibility(View.INVISIBLE);
    q5.setVisibility(View.INVISIBLE);
    q6.setVisibility(View.INVISIBLE);
    q7.setVisibility(View.INVISIBLE);
    q8.setVisibility(View.INVISIBLE);
    q9.setVisibility(View.INVISIBLE);
    q10.setVisibility(View.INVISIBLE);
    q11.setVisibility(View.INVISIBLE);
    q12.setVisibility(View.INVISIBLE);
    tick.setVisibility(View.INVISIBLE);
    final Handler handler = new Handler();
    qA1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (Qlist.isEmpty()) {
                Intent res = new Intent(Questions.this, Result.class);
                startActivity(res);
            } else {
                tick.setVisibility(View.VISIBLE);
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        tick.setVisibility(View.GONE);
                        if (Qlist.indexOf(0) == Qlist.size()) {
                            randomStr = (String) Qlist.get(new Random().nextInt(Qlist.size() - 1));
                            textView.setText(randomStr);
                        } else {
                            Qlist.remove(randomStr);
                            randomStr = (String) Qlist.get(new Random().nextInt(Qlist.size() - 1));
                            textView.setText(randomStr);
                            correct++;
                        }
                    }
                }, 1000);
            }
        }
    });


}

public void Question2() {

    q1.setVisibility(View.INVISIBLE);
    q2.setVisibility(View.VISIBLE);
    q3.setVisibility(View.INVISIBLE);
    q4.setVisibility(View.INVISIBLE);
    q5.setVisibility(View.INVISIBLE);
    q6.setVisibility(View.INVISIBLE);
    q7.setVisibility(View.INVISIBLE);
    q8.setVisibility(View.INVISIBLE);
    q9.setVisibility(View.INVISIBLE);
    q10.setVisibility(View.INVISIBLE);
    q11.setVisibility(View.INVISIBLE);
    q12.setVisibility(View.INVISIBLE);
    tick.setVisibility(View.INVISIBLE);
    final Handler handler = new Handler();
    qA2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (Qlist.size() == 0) {
                Intent res = new Intent(Questions.this, Result.class);
                startActivity(res);
            } else {
                tick.setVisibility(View.VISIBLE);

                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {

                        tick.setVisibility(View.GONE);
                        if (Qlist.indexOf(1) == Qlist.size()) {
                            randomStr = (String) Qlist.get(new Random().nextInt(Qlist.size() - 1));
                            textView.setText(randomStr);
                        } else {
                            Qlist.remove(randomStr);
                            randomStr = (String) Qlist.get(new Random().nextInt(Qlist.size() - 1));
                            textView.setText(randomStr);
                            correct++;
                        }
                    }
                }, 1000);
            }
        }
    });
}

public void Question3() {

    q1.setVisibility(View.INVISIBLE);
    q2.setVisibility(View.INVISIBLE);
    q4.setVisibility(View.INVISIBLE);
    q3.setVisibility(View.VISIBLE);
    q5.setVisibility(View.INVISIBLE);
    q6.setVisibility(View.INVISIBLE);
    q7.setVisibility(View.INVISIBLE);
    q8.setVisibility(View.INVISIBLE);
    q9.setVisibility(View.INVISIBLE);
    q10.setVisibility(View.INVISIBLE);
    q11.setVisibility(View.INVISIBLE);
    q12.setVisibility(View.INVISIBLE);
    tick.setVisibility(View.INVISIBLE);
    final Handler handler = new Handler();
    qA3.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            tick.setVisibility(View.VISIBLE);
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {

                    tick.setVisibility(View.GONE);
                    if (Qlist.indexOf(2) == Qlist.size()) {
                        randomStr = (String) Qlist.get(new Random().nextInt(Qlist.size() - 1));
                        textView.setText(randomStr);
                    } else {
                        Qlist.remove(randomStr);
                        randomStr = (quest[new Random().nextInt(quest.length - 1)]);
                        textView.setText(randomStr);
                        correct++;
                    }
                }
            }, 1000);
        }

    });
}

public void Question4() {

    q1.setVisibility(View.INVISIBLE);
    q2.setVisibility(View.INVISIBLE);
    q3.setVisibility(View.INVISIBLE);
    q5.setVisibility(View.INVISIBLE);
    q4.setVisibility(View.VISIBLE);
    q6.setVisibility(View.INVISIBLE);
    q7.setVisibility(View.INVISIBLE);
    q8.setVisibility(View.INVISIBLE);
    q9.setVisibility(View.INVISIBLE);
    q10.setVisibility(View.INVISIBLE);
    q11.setVisibility(View.INVISIBLE);
    q12.setVisibility(View.INVISIBLE);
    tick.setVisibility(View.INVISIBLE);
    final Handler handler = new Handler();
    qA4.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (Qlist.size() == 0) {
                Intent res = new Intent(Questions.this, Result.class);
                startActivity(res);
            } else {
                tick.setVisibility(View.VISIBLE);
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        tick.setVisibility(View.GONE);
                        if (Qlist.indexOf(3) == Qlist.size()) {
                            randomStr = (String) Qlist.get(new Random().nextInt(Qlist.size() - 1));
                            textView.setText(randomStr);
                        } else {
                            Qlist.remove(randomStr);
                            randomStr = (String) Qlist.get(new Random().nextInt(Qlist.size() - 1));
                            textView.setText(randomStr);
                            correct++;
                        }
                    }
                }, 1000);
            }
        }
    });
}


public void Question5() {

    q1.setVisibility(View.INVISIBLE);
    q2.setVisibility(View.INVISIBLE);
    q3.setVisibility(View.INVISIBLE);
    q4.setVisibility(View.INVISIBLE);
    q6.setVisibility(View.INVISIBLE);
    q5.setVisibility(View.VISIBLE);
    q7.setVisibility(View.INVISIBLE);
    q8.setVisibility(View.INVISIBLE);
    q9.setVisibility(View.INVISIBLE);
    q10.setVisibility(View.INVISIBLE);
    q11.setVisibility(View.INVISIBLE);
    q12.setVisibility(View.INVISIBLE);
    tick.setVisibility(View.INVISIBLE);
    final Handler handler = new Handler();
    qA5.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            tick.setVisibility(View.VISIBLE);
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {

                    tick.setVisibility(View.GONE);
                    if (Qlist.indexOf(4) == Qlist.size()) {
                        randomStr = (String) Qlist.get(new Random().nextInt(Qlist.size() - 1));
                        textView.setText(randomStr);
                    } else {
                        Qlist.remove(randomStr);
                        randomStr = (String) Qlist.get(new Random().nextInt(Qlist.size() - 1));
                        textView.setText(randomStr);
                        correct++;
                    }
                }
            }, 1000);
        }

    });

}

public void Question6() {

    q1.setVisibility(View.INVISIBLE);
    q2.setVisibility(View.INVISIBLE);
    q3.setVisibility(View.INVISIBLE);
    q4.setVisibility(View.INVISIBLE);
    q5.setVisibility(View.INVISIBLE);
    q7.setVisibility(View.INVISIBLE);
    q6.setVisibility(View.VISIBLE);
    q8.setVisibility(View.INVISIBLE);
    q9.setVisibility(View.INVISIBLE);
    q10.setVisibility(View.INVISIBLE);
    q11.setVisibility(View.INVISIBLE);
    q12.setVisibility(View.INVISIBLE);
    tick.setVisibility(View.INVISIBLE);
    final Handler handler = new Handler();
    qA6.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (Qlist.size() == 0) {
                Intent res = new Intent(Questions.this, Result.class);
                startActivity(res);
            } else {
                tick.setVisibility(View.VISIBLE);
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        tick.setVisibility(View.GONE);
                        if (Qlist.indexOf(5) == Qlist.size()) {
                            randomStr = (String) Qlist.get(new Random().nextInt(Qlist.size() - 1));
                            textView.setText(randomStr);
                        } else {
                            Qlist.remove(randomStr);
                            randomStr = (String) Qlist.get(new Random().nextInt(Qlist.size() - 1));
                            textView.setText(randomStr);
                            correct++;
                        }
                    }
                }, 1000);
            }
        }
    });
}

public void Question7() {

    q1.setVisibility(View.INVISIBLE);
    q2.setVisibility(View.INVISIBLE);
    q3.setVisibility(View.INVISIBLE);
    q4.setVisibility(View.INVISIBLE);
    q5.setVisibility(View.INVISIBLE);
    q6.setVisibility(View.INVISIBLE);
    q8.setVisibility(View.INVISIBLE);
    q7.setVisibility(View.VISIBLE);
    q9.setVisibility(View.INVISIBLE);
    q10.setVisibility(View.INVISIBLE);
    q11.setVisibility(View.INVISIBLE);
    q12.setVisibility(View.INVISIBLE);
    tick.setVisibility(View.INVISIBLE);
    final Handler handler = new Handler();
    qA7.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (Qlist.size() == 0) {
                Intent res = new Intent(Questions.this, Result.class);
                startActivity(res);
            } else {
                tick.setVisibility(View.VISIBLE);
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        tick.setVisibility(View.GONE);
                        if (Qlist.indexOf(6) == Qlist.size()) {
                            randomStr = (String) Qlist.get(new Random().nextInt(Qlist.size() - 1));
                            textView.setText(randomStr);
                        } else {
                            Qlist.remove(randomStr);
                            randomStr = (quest[new Random().nextInt(quest.length - 1)]);
                            textView.setText(randomStr);
                            correct++;
                        }
                    }
                }, 1000);
            }
        }
    });

}

public void Question8() {

    q1.setVisibility(View.INVISIBLE);
    q2.setVisibility(View.INVISIBLE);
    q3.setVisibility(View.INVISIBLE);
    q4.setVisibility(View.INVISIBLE);
    q5.setVisibility(View.INVISIBLE);
    q6.setVisibility(View.INVISIBLE);
    q7.setVisibility(View.INVISIBLE);
    q9.setVisibility(View.INVISIBLE);
    q8.setVisibility(View.VISIBLE);
    q10.setVisibility(View.INVISIBLE);
    q11.setVisibility(View.INVISIBLE);
    q12.setVisibility(View.INVISIBLE);
    tick.setVisibility(View.INVISIBLE);
    final Handler handler = new Handler();
    qA8.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (Qlist.size() == 0) {
                Intent res = new Intent(Questions.this, Result.class);
                startActivity(res);
            } else {
                tick.setVisibility(View.VISIBLE);
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {

                        tick.setVisibility(View.GONE);
                        if (Qlist.indexOf(7) == Qlist.size()) {
                            randomStr = (String) Qlist.get(new Random().nextInt(Qlist.size() - 1));
                            textView.setText(randomStr);
                        } else {
                            Qlist.remove(randomStr);
                            randomStr = (String) Qlist.get(new Random().nextInt(Qlist.size() - 1));
                            textView.setText(randomStr);
                            correct++;
                        }
                    }
                }, 1000);
            }
        }
    });
}

public void Question9() {

    q1.setVisibility(View.INVISIBLE);
    q2.setVisibility(View.INVISIBLE);
    q3.setVisibility(View.INVISIBLE);
    q4.setVisibility(View.INVISIBLE);
    q5.setVisibility(View.INVISIBLE);
    q6.setVisibility(View.INVISIBLE);
    q7.setVisibility(View.INVISIBLE);
    q8.setVisibility(View.INVISIBLE);
    q9.setVisibility(View.VISIBLE);
    q10.setVisibility(View.INVISIBLE);
    q11.setVisibility(View.INVISIBLE);
    q12.setVisibility(View.INVISIBLE);
    tick.setVisibility(View.INVISIBLE);
    final Handler handler = new Handler();
    qA9.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (Qlist.size() == 0) {
                Intent res = new Intent(Questions.this, Result.class);
                startActivity(res);
            } else {

                tick.setVisibility(View.VISIBLE);
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {

                        tick.setVisibility(View.GONE);
                        if (Qlist.indexOf(8) == Qlist.size()) {
                            randomStr = (String) Qlist.get(new Random().nextInt(Qlist.size() - 1));
                            textView.setText(randomStr);
                        } else {
                            Qlist.remove(randomStr);
                            randomStr = (String) Qlist.get(new Random().nextInt(Qlist.size() - 1));
                            textView.setText(randomStr);
                            correct++;
                        }
                    }
                }, 1000);
            }
        }
    });
}

public void Question10() {

    q1.setVisibility(View.INVISIBLE);
    q2.setVisibility(View.INVISIBLE);
    q3.setVisibility(View.INVISIBLE);
    q4.setVisibility(View.INVISIBLE);
    q5.setVisibility(View.INVISIBLE);
    q6.setVisibility(View.INVISIBLE);
    q7.setVisibility(View.INVISIBLE);
    q8.setVisibility(View.INVISIBLE);
    q9.setVisibility(View.INVISIBLE);
    q10.setVisibility(View.VISIBLE);
    q11.setVisibility(View.INVISIBLE);
    q12.setVisibility(View.INVISIBLE);
    tick.setVisibility(View.INVISIBLE);
    final Handler handler = new Handler();
    qA10.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (Qlist.size() == 0) {
                Intent res = new Intent(Questions.this, Result.class);
                startActivity(res);
            } else {
                tick.setVisibility(View.VISIBLE);
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {

                        tick.setVisibility(View.GONE);
                        if (Qlist.indexOf(9) == Qlist.size()) {
                            randomStr = (String) Qlist.get(new Random().nextInt(Qlist.size() - 1));
                            textView.setText(randomStr);
                        } else {
                            Qlist.remove(randomStr);
                            randomStr = (String) Qlist.get(new Random().nextInt(Qlist.size() - 1));
                            textView.setText(randomStr);
                            correct++;
                        }
                    }
                }, 1000);
            }
        }
    });
}

public void Question11() {

    q1.setVisibility(View.INVISIBLE);
    q2.setVisibility(View.INVISIBLE);
    q3.setVisibility(View.INVISIBLE);
    q4.setVisibility(View.INVISIBLE);
    q5.setVisibility(View.INVISIBLE);
    q6.setVisibility(View.INVISIBLE);
    q7.setVisibility(View.INVISIBLE);
    q8.setVisibility(View.INVISIBLE);
    q9.setVisibility(View.INVISIBLE);
    q10.setVisibility(View.INVISIBLE);
    q11.setVisibility(View.VISIBLE);
    q12.setVisibility(View.INVISIBLE);
    tick.setVisibility(View.INVISIBLE);
    final Handler handler = new Handler();
    qA11.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (Qlist.size() == 0) {
                Intent res = new Intent(Questions.this, Result.class);
                startActivity(res);
            } else {
                tick.setVisibility(View.VISIBLE);
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {

                        tick.setVisibility(View.GONE);
                        if (Qlist.indexOf(10) == Qlist.size()) {
                            randomStr = (String) Qlist.get(new Random().nextInt(Qlist.size() - 1));
                            textView.setText(randomStr);
                        } else {
                            Qlist.remove(randomStr);
                            randomStr = (String) Qlist.get(new Random().nextInt(Qlist.size() - 1));
                            textView.setText(randomStr);
                            correct++;
                        }
                    }
                }, 1000);
            }

        }
    });

}


public void Question12() {

    q1.setVisibility(View.INVISIBLE);
    q2.setVisibility(View.INVISIBLE);
    q3.setVisibility(View.INVISIBLE);
    q4.setVisibility(View.INVISIBLE);
    q5.setVisibility(View.INVISIBLE);
    q6.setVisibility(View.INVISIBLE);
    q7.setVisibility(View.INVISIBLE);
    q8.setVisibility(View.INVISIBLE);
    q9.setVisibility(View.INVISIBLE);
    q10.setVisibility(View.INVISIBLE);
    q11.setVisibility(View.INVISIBLE);
    q12.setVisibility(View.VISIBLE);
    tick.setVisibility(View.INVISIBLE);
    final Handler handler = new Handler();
    qA12.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (Qlist.size() == 0) {
                Intent res = new Intent(Questions.this, Result.class);
                startActivity(res);
            } else {
                tick.setVisibility(View.VISIBLE);
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        tick.setVisibility(View.GONE);
                        if (Qlist.indexOf(11) == Qlist.size()) {
                            randomStr = (String) Qlist.get(new Random().nextInt(Qlist.size() - 1));
                            textView.setText(randomStr);
                        } else {
                            Qlist.remove(randomStr);
                            randomStr = (String) Qlist.get(new Random().nextInt(Qlist.size() - 1));
                            textView.setText(randomStr);
                            correct++;
                        }
                    }
                }, 1000);
            }

        }
    });
}

}




Aucun commentaire:

Enregistrer un commentaire