我收到了一份崩溃报告,大约是java.lang.StringIndexOutOfBoundsException in ZhuangDictActivity$SearchDicAsyncTask.doInBackground
这是ZhuangDictActivity $ SearchDicAsyncTask.doInBackground:
private class SearchDicAsyncTask extends AsyncTask<String, Integer, String> {
private byte searchStatus;
@Override
protected String doInBackground(String... params) {
if (params[0].length() > 0) {
word = params[0].trim();
long[] index = null;
FileAccessor in = null;
DictZipInputStream din = null;
try {
char key = GB2Alpha.Char2Alpha(word.charAt(0));
tableName = DatabaseHelper.transTableName(key);
index = databaseHelper.queryTable(tableName, word);
if (index != null) {
in = new FileAccessor(new File(dictFileName), "r");
byte[] bytes = new byte[(int) index[1]];
if (isDZDict) {
din = new DictZipInputStream(in);
DictZipHeader h = din.readHeader();
int idx = (int) index[0] / h.getChunkLength();
int off = (int) index[0] % h.getChunkLength();
long pos = h.getOffsets()[idx];
in.seek(pos);
byte[] b = new byte[off + (int) index[1]];
din.readFully(b);
System.arraycopy(b, off, bytes, 0, (int) index[1]);
} else {
in.seek(index[0]);
in.read(bytes);
}
wordDefinition = new String(bytes, "UTF-8");
} else {
searchStatus = 0;
return null;
}
} catch (FileNotFoundException ffe) {
searchStatus = 1;
return null;
} catch (IOException ex) {
ex.printStackTrace();
searchStatus = 2;
return null;
} finally {
try {
if (din != null)
din.close();
if (in != null)
in.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
return wordDefinition;
}
}
完整的代码可用here。
我在Java和Android开发方面的知识有限。我该怎么解决这个问题?我打算发布完整的堆栈跟踪,但stackoverflow不允许我这样做,因为它说我的问题有太多的代码。无论如何,导致问题的一行是char key = GB2Alpha.Char2Alpha(word.charAt(0));
。
答案 0 :(得分:0)
您的字符串可能只包含空格。意味着它通过了条件:
if (params[0].length() > 0)
但是当你调用trim()时,会删除它们,导致一个空流,并在执行时抛出“IndexOutOfBoundsException”异常:
word.charAt(0)
修改强>
这不是原因。在测试之后,当对只有空格的String调用trim时,String保持不变。