我在提取字符串时遇到了一些问题
我正在使用 4 个选项(例如按钮)进行多项选择,通过引用文件名进行选择。该文件(即问题)是一个png,文件名是Number-Q01AZ7BZ8CZ9DZ10ANZ8.png。这些 png 放在 assets 文件夹下。
Set<String> regions = regionsMap.keySet(); // get Set of regions
// loop through each region
for (String region : regions)
{
if (regionsMap.get(region)) // if region is enabled
{
// 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", ""));
} // end if
} // end for
String fileName = fileNameList.get(randomIndex);
if (!quizCountriesList.contains(fileName))
{
quizCountriesList.add(fileName); // add the file to the list
String nextImageName = quizCountriesList.remove(0);
correctAnswer = nextImageName; // update the correct answer
int AZ = correctAnswer.indexOf("AZ");
int BZ = correctAnswer.indexOf("BZ");
int CZ = correctAnswer.indexOf("CZ");
int DZ = correctAnswer.indexOf("DZ");
int ANZ = correctAnswer.indexOf("ANZ");
String choiceA = null;
String choiceB = null;
String choiceC = null;
String choiceD = null;
choiceA = correctAnswer.substring( (AZ+2), (BZ) );
choiceB = correctAnswer.substring( (BZ+2), (CZ) );
choiceC = correctAnswer.substring( (CZ+2), (DZ) );
choiceD = correctAnswer.substring( (DZ+2), (ANZ) );
日志猫如下:
11-09 21:14:08.495: E/AndroidRuntime(25905): FATAL EXCEPTION: main
11-09 21:14:08.495: E/AndroidRuntime(25905): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.trial.quizgame/com.trial.quizgame.QuizGame}: java.lang.StringIndexOutOfBoundsException: length=15; regionStart=1; regionLength=-2
11-09 21:14:08.495: E/AndroidRuntime(25905): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1967)
我试图将按钮设置为 .setText(correctAnswer) 并且它将正确显示为 Number-Q01AZ7BZ8CZ9DZ10ANZ8,因此获取“correctAnswer”字符串的顶部应该没问题。提取字符串时留下的问题,但 BZ 必须位于 AZ 后面的位置,例如 CZ 在 BZ 后面,等等:
从 logcat 的 regionLength 是-2?我怎么能处理这个?我希望它适用于 Q01,选择 A=7、B=8、C=9、D=10 和 ANZ=8
提前感谢您的建议!