【发布时间】:2012-12-25 11:38:31
【问题描述】:
我有这个功能:
public static String read(Context context) {
FileInputStream fis;
try {
fis = context.openFileInput(INFO_FILE_NAME);
int bufferSize = 2 * 1024 * 1024; // 2 MiB
byte[] buffer = new byte[bufferSize];
fis.read(buffer);
return new String(buffer);
} catch (Exception e) {
Log.d(TAG,
"Exception while reading General Information File:"
+ e.getMessage());
return null;
}
}
函数返回空值。这意味着有一个例外。但是,没有任何内容记录到 LogCat。
我尝试使用 Eclipse 对其进行调试。一切正常,直到“return new String(buffer);”线。当我尝试执行它时,调试器直接跳转到“return null;”行,无需通过“Log.d”。另外,当我在“return null;”时行,我在调试器中看不到变量“e”。怎么会这样?
【问题讨论】:
-
如果 ricintech 的回答不起作用,那么您生成的字节肯定有问题......
-
看起来好像是编码有问题:如果我添加编码,不会产生异常。但是:1)虽然我不能记录异常和 2)哪个是正确的编码?如果我将文件传输到我的 PC,Notepad++ 将其打开为 UTF-8 没有 BOM 并显示它正常。但是,如果我使用 new String(buffer, "UTF-8"),非 ASCII 字符将无法正确显示。
-
我终于发现唯一的问题是“return new String(buffer);”中缺少编码线,正如你所指出的。如果您想发表评论作为答案,我会接受。感谢您的帮助。
-
我已经发布了我的答案:)
标签: android string debugging exception