我不能从另一个类获取arrayList

时间:2015-10-11 22:33:20

标签: java android arraylist

我不知道为什么我不能从另一个班级获得ArrayList

第一堂课中的方法获得ArrayList并将文字设置为四TextView

private void GameStart() {
    ActivityStartGame startGame = new ActivityStartGame();
    startGame.GameStart();

    ArrayList<String> getWordList = startGame.getPutWords();
    String[] putWords = new String[getWordList.size()];
    for (int i = 0; i < getWordList.size(); i++) {
        putWords[i] = getWordList.get(i);
    }
    txtSelect1.setText(putWords[1]);
    txtSelect2.setText(putWords[2]);
    txtSelect3.setText(putWords[3]);
    txtSelect4.setText(putWords[4]);
}

第二类在四个随机位置创建四个随机单词

private String[] wordsEnglish = new String[] { "A", "B", "C",
        "D", "E", "F", "G", "H", "I" };

private String[] wordsFarsi = new String[] { "4", "3", "2", "1",
        "9", "8", "7", "6", "5" };

private ArrayList<Integer> chosenWordNumbers = new ArrayList<Integer>();
private ArrayList<String> putWords = new ArrayList<String>();

public void GameStart() {
    Thread thread = new Thread(new Runnable() {

        @Override
        public void run() {
            doStart();
        }

        private void doStart() {

            if (!part1) {

                randomNumber = (int) (Math.random() * wordsEnglish.length);
                currentwordFarsi = wordsFarsi[randomNumber];
                currentwordEnglish = wordsEnglish[randomNumber];
                answer1 = wordsFarsi[randomNumber];
                putSelect();
                putWords.add(randomNumberSelect, answer1);

                part1 = true;

            }
            if (!part2) {

                randomNumber = (int) (Math.random() * wordsFarsi.length);
                currentwordFarsi = wordsFarsi[randomNumber];
                if (answer1 == currentwordFarsi) {
                    GameStart();
                }
                answer2 = wordsFarsi[randomNumber];
                putSelect();
                putWords.add(randomNumberSelect, answer2);
                part2 = true;

            }
            if (!part3) {

                randomNumber = (int) (Math.random() * wordsFarsi.length);
                currentwordFarsi = wordsFarsi[randomNumber];
                if (answer1 == currentwordFarsi) {
                    GameStart();
                } else if (answer2 == currentwordFarsi) {
                    GameStart();
                }
                answer3 = wordsFarsi[randomNumber];
                putSelect();
                putWords.add(randomNumberSelect, answer3);
                part3 = true;

            }
            if (!part4) {

                randomNumber = (int) (Math.random() * wordsFarsi.length);
                currentwordFarsi = wordsFarsi[randomNumber];
                if (answer1 == currentwordFarsi) {
                    GameStart();
                } else if (answer2 == currentwordFarsi) {
                    GameStart();
                } else if (answer3 == currentwordFarsi) {
                    GameStart();
                }
                answer4 = wordsFarsi[randomNumber];
                putSelect();
                putWords.add(randomNumberSelect, answer4);
                part4 = true;

            }
          thread.start();
        }

        private void putSelect() {
            do {

                randomNumberSelect = (int) ((Math.random() * 4) + 1);
                if (!chosenWordNumbers.contains(randomNumberSelect)) {
                    chosenWordNumbers.add(randomNumberSelect,
                            randomNumberSelect);

                    break;
                }

            } while (true);
        }

    });
}

public ArrayList<String> getPutWords() {

    return putWords;

}

这是我写的代码。我有一个错误,我不知道我的问题是在获取ArrayList还是在第二个类中创建ArrayList

编辑:我检查过......我的问题出在方法putSelect的第二课!

1 个答案:

答案 0 :(得分:0)

在我看来,你想要分配文本的第一堂课中的putWords索引有错误。您尝试访问索引1到4 ..但是大小为4的数组具有索引0到3。