gpt4 book ai didi

android - java.lang.RuntimeException : An error occured while executing doInBackground() in Android 错误

转载 作者:行者123 更新时间:2023-11-29 20:11:37 24 4
gpt4 key购买 nike

我正在开发一个应用程序,用户可以在其中销售或购买二手元素。在我的第一个 Activity 中,我有 GridView ,我在其中显示缩略图,当用户单击图像时,它会将他带到详细 Activity 。我使用后退按钮返回主 Activity 页面。它工作正常,但如果我单击 8-9 个图像并出现内存不足 (OOM),它就会崩溃。

CustomVolleyRequest 类

public class CustomVolleyRequest {

private static CustomVolleyRequest customVolleyRequest;
private static Context context;
private RequestQueue requestQueue;
private ImageLoader imageLoader;


private CustomVolleyRequest(Context context) {
this.context = context;
this.requestQueue = getRequestQueue();

imageLoader = new ImageLoader(requestQueue,
new ImageLoader.ImageCache() {
private final LruCache<String, Bitmap>
cache = new LruCache<String, Bitmap>(20);

@Override
public Bitmap getBitmap(String url) {
return cache.get(url);
}

@Override
public void putBitmap(String url, Bitmap bitmap) {
cache.put(url, bitmap);
}
});
}

public static synchronized CustomVolleyRequest getInstance(Context context) {
if (customVolleyRequest == null) {
customVolleyRequest = new CustomVolleyRequest(context);
}
return customVolleyRequest;
}

public RequestQueue getRequestQueue() {
if (requestQueue == null) {
Cache cache = new DiskBasedCache(context.getCacheDir(), 10 * 1024 * 1024);
Network network = new BasicNetwork(new HurlStack());
requestQueue = new RequestQueue(cache, network);
requestQueue.start();
}
return requestQueue;
}

public ImageLoader getImageLoader() {
return imageLoader;
}

}

内存监控,每次点击都会增加,当我返回主 Activity 时它会释放内存!

enter image description here

堆栈跟踪:

E/AndroidRuntime: FATAL EXCEPTION: AsyncTask  E/AndroidRuntime: 
Process: PID: 5700
java.lang.RuntimeException: An error occured while executing
doInBackground() E/AndroidRuntime: at
android.os.AsyncTask$3.done(AsyncTask.java:300) E/AndroidRuntime: at
java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
E/AndroidRuntime: at
java.util.concurrent.FutureTask.setException(FutureTask.java:222)
E/AndroidRuntime: at
java.util.concurrent.FutureTask.run(FutureTask.java:242)
E/AndroidRuntime: at
android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
E/AndroidRuntime: at
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
E/AndroidRuntime: at
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
E/AndroidRuntime: at java.lang.Thread.run(Thread.java:841)
E/AndroidRuntime: Caused by: java.lang.OutOfMemoryError
E/AndroidRuntime: at
android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
E/AndroidRuntime: at
android.graphics.BitmapFactory.decodeStreamInternal(BitmapFactory.java:613)
E/AndroidRuntime: at
android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:589)
E/AndroidRuntime: at
android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:627)
E/AndroidRuntime: at
com.example.xxx.xxx.DownloadImageTask.doInBackground(DownloadImageTask.java:26)
E/AndroidRuntime: at
com.example.xxx.xxx.DownloadImageTask.doInBackground(DownloadImageTask.java:14)
E/AndroidRuntime: at
android.os.AsyncTask$2.call(AsyncTask.java:288)
E/AndroidRuntime: at
java.util.concurrent.FutureTask.run(FutureTask.java:237)
E/AndroidRuntime: at
android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
E/AndroidRuntime: at
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
E/AndroidRuntime: at
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
E/AndroidRuntime: at java.lang.Thread.run(Thread.java:841)

最佳答案

根据日志,查看这一行:

E/AndroidRuntime:  Caused by: java.lang.OutOfMemoryError

它清楚地表明应用程序崩溃只是因为 OutOfMemoryError 异常。一个可能的原因可能是您正在处理大尺寸位图而没有缩小它们的比例。在将位图设置为 ImageView 之前,您需要调整大小和缩小位图。

有关如何有效管理位图的更多详细信息,请参阅官方 docs

关于android - java.lang.RuntimeException : An error occured while executing doInBackground() in Android 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34736674/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com