基本上我所做的是一个标志测验应用程序,可以测试你猜对了多少个标志,并在最后给你一个基于百分比的分数。
问题在于,当我进行测验时,我有时会出现outOfBounds错误,应用程序崩溃而不是显示分数。它是相当随机的,因为有时我可以做3-4次应用程序没有问题,最后正确显示得分。
这些是在最后重置测验时使用的2个类,问题是String nextImage = quizCountriesList.remove(0);
内的loadNextFlag()
确切的错误是:
在 java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
at java.util.ArrayList.remove(ArrayList.java:403)
在 com.map524s1a.flagquizdk.MainActivityFragment.loadNextFlag(MainActivityFragment.java:180)
public void resetQuiz() {
// use AssetManager to get image file names for enabled regions
AssetManager assets = getActivity().getAssets();
fileNameList.clear(); // empty list of image file names
try {
// loop through each region
for (String region : regionsSet) {
// get a list of all flag image files in this region
String[] paths = assets.list(region);
for (String path : paths)
fileNameList.add(path.replace(".png", ""));
}
} catch (IOException ex) {
Log.e(TAG, "Error loading image file names", ex);
}
correctAnswers = 0; // reset the number of correct answers made
questions = 0; // reset the total number of questions
quizCountriesList.clear(); // clear prior list of quiz countries
int flagCounter = 1;
int numberOfFlags = fileNameList.size();
// add FLAGS_IN_QUIZ random file names to the quizCountriesList
while (flagCounter <= FLAGS_IN_QUIZ) {
int randomIndex = random.nextInt(numberOfFlags);
// get the random file name
String filename = fileNameList.get(randomIndex);
// if the region is enabled and it hasn't already been chosen
if (!quizCountriesList.contains(filename)) {
quizCountriesList.add(filename); // add the file to the list
++flagCounter;
}
}
loadNextFlag(); // start the quiz by loading the first flag
}
// after the user guesses a correct flag, load the next flag
private void loadNextFlag() {
// get file name of the next flag and remove it from the list
String nextImage = quizCountriesList.remove(0);
correctAnswer = nextImage; // update the correct answer
answerTextView.setText(""); // clear answerTextView
// display current question number
questionNumberTextView.setText(getString(R.string.question, (questions + 1), FLAGS_IN_QUIZ));
// extract the region from the next image's name
String region = nextImage.substring(0, nextImage.indexOf('-'));
// use AssetManager to load next image from assets folder
AssetManager assets = getActivity().getAssets();
// get an InputStream to the asset representing the next flag
// and try to use the InputStream
try (InputStream stream =
assets.open(region + "/" + nextImage + ".png")) {
// load the asset as a Drawable and display on the flagImageView
Drawable flag = Drawable.createFromStream(stream, nextImage);
flagImageView.setImageDrawable(flag);
animate(false); // animate the flag onto the screen
}
catch (IOException exception) {
Log.e(TAG, "Error loading " + nextImage, exception);
}
Collections.shuffle(fileNameList); // shuffle file names
// put the correct answer at the end of fileNameList
int correct = fileNameList.indexOf(correctAnswer);
fileNameList.add(fileNameList.remove(correct));
// add 2, 4, 6 or 8 guess Buttons based on the value of guessRows
for (int row = 0; row < guessRows; row++) {
// place Buttons in currentTableRow
for (int column = 0;
column < guessLinearLayouts[row].getChildCount();
column++) {
// get reference to Button to configure
Button newGuessButton =
(Button) guessLinearLayouts[row].getChildAt(column);
newGuessButton.setEnabled(true);
// get country name and set it as newGuessButton's text
String filename = fileNameList.get((row * 2) + column);
newGuessButton.setText(getCountryName(filename));
}
}
// randomly replace one Button with the correct answer
int row = random.nextInt(guessRows); // pick random row
int column = random.nextInt(2); // pick random column
LinearLayout randomRow = guessLinearLayouts[row]; // get the row
String countryName = getCountryName(correctAnswer);
((Button) randomRow.getChildAt(column)).setText(countryName);
}