0

我收到了一份崩溃报告,它是关于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;
        }  
    }

完整的代码可在此处获得。我在 Java 和 Android 开发方面的知识有限。我应该如何解决这个问题?我打算发布完整的堆栈跟踪,但 stackoverflow 不允许我这样做,因为它表明我的问题有太多代码。无论如何,导致问题的行是char key = GB2Alpha.Char2Alpha(word.charAt(0));.

4

1 回答 1

0

您的字符串可能只包含空格。意味着它通过了条件:

if (params[0].length() > 0)

但是当您调用 trim() 时,这些会被删除,从而导致空流和执行时抛出“IndexOutOfBoundsException”异常:

word.charAt(0)

编辑

这不是原因。测试后,当对仅包含空格的字符串调用 trim 时,字符串保持不变。

于 2013-11-11T14:13:18.853 回答