我面临这个问题:OutOfMemoryError!!
这是导致问题的屏幕:
当我使用鼠标向下滚动时。我的应用程序突然关闭,这是 logcat 显示的内容:
09-05 08:25:36.469: E/AndroidRuntime(8941): FATAL EXCEPTION: AsyncTask #5
09-05 08:25:36.469: E/AndroidRuntime(8941): java.lang.RuntimeException: An error occured while executing doInBackground()
09-05 08:25:36.469: E/AndroidRuntime(8941): at android.os.AsyncTask$3.done(AsyncTask.java:299)
09-05 08:25:36.469: E/AndroidRuntime(8941): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
09-05 08:25:36.469: E/AndroidRuntime(8941): at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
09-05 08:25:36.469: E/AndroidRuntime(8941): at java.util.concurrent.FutureTask.run(FutureTask.java:239)
09-05 08:25:36.469: E/AndroidRuntime(8941): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
09-05 08:25:36.469: E/AndroidRuntime(8941): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
09-05 08:25:36.469: E/AndroidRuntime(8941): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
09-05 08:25:36.469: E/AndroidRuntime(8941): at java.lang.Thread.run(Thread.java:841)
09-05 08:25:36.469: E/AndroidRuntime(8941): Caused by: java.lang.OutOfMemoryError
09-05 08:25:36.469: E/AndroidRuntime(8941): at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
09-05 08:25:36.469: E/AndroidRuntime(8941): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:530)
09-05 08:25:36.469: E/AndroidRuntime(8941): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:603)
09-05 08:25:36.469: E/AndroidRuntime(8941): at com.ptit.piclient.tools.AllSongAdapter2$DownloadImageTask.doInBackground(AllSongAdapter2.java:91)
09-05 08:25:36.469: E/AndroidRuntime(8941): at com.ptit.piclient.tools.AllSongAdapter2$DownloadImageTask.doInBackground(AllSongAdapter2.java:1)
09-05 08:25:36.469: E/AndroidRuntime(8941): at android.os.AsyncTask$2.call(AsyncTask.java:287)
09-05 08:25:36.469: E/AndroidRuntime(8941): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
09-05 08:25:36.469: E/AndroidRuntime(8941): ... 4 more
它说问题是DownloadImageTask
班级。这是该类的代码:
private class DownloadImageTask extends AsyncTask<String, Void, Bitmap>{
ImageView imgView;
public DownloadImageTask(ImageView img){this.imgView = img;}
@Override
protected Bitmap doInBackground(String... urls) {
String urlDisplay = urls[0];
Bitmap icon = null;
InputStream in = null ;
try {
in = new URL(urlDisplay).openStream();
icon = BitmapFactory.decodeStream(in);
} catch (Exception e) {
e.printStackTrace();
}
//khuongdv start sep28
finally{
try {
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//khuongdv end sep28
return icon;
}
protected void onPostExecute(Bitmap result){
if(result == null) return;
imgView.setImageBitmap(result);
}
}
你能指出这是为什么以及如何解决这个问题!?
谢谢!