我在LogCat中收到 OutOfMemoryError消息,我的應用程序崩潰,因爲錯誤未被捕獲。
引起 OutOfMemoryError的兩件事:
1.將大文本文件讀入字符串。
2.將該字符串發送到我的TextView。
簡單地在這兩件事情上添加一個catch不僅捕獲 OutOfMemoryError,但似乎完全解決了內存不足的問題。
沒有更多的崩潰和LogCat中沒有更多的錯誤消息。該應用程序運行完美。
這怎麼可能?究竟發生了什麼?
'捕捉'OutOfMemoryError完全解決了內存不足的問題?
有了這個代碼,我得到的錯誤信息&應用程序崩潰:
try
{
myString = new Scanner(new File(myFilePath)).useDelimiter("\\A").next();
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
myTextView.setText(myString);
僅僅通過 '追趕' 的的OutOfMemoryError,在logcat中沒有更多的錯誤消息,並沒有崩潰:
try
{
myString = new Scanner(new File(myFilePath)).useDelimiter("\\A").next();
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch(OutOfMemoryError e)
{
}
try
{
myTextView.setText(myString);
}
catch(OutOfMemoryError e)
{
}
以查看日誌在OutOfMemoryError catch塊中添加e.printstacktrace – Calvin
同意@Droider。您沒有看到錯誤消息,因爲您沒有像以前那樣打印它們。 – csmckelvey
請參閱http://stackoverflow.com/questions/2679330/catching-java-lang-outofmemoryerror – Raedwald