gpt4 book ai didi

android - NoSuchMethodError 在 Android 中使用 LruCache

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:06:19 25 4
gpt4 key购买 nike

我在使用从 Android 中的 LruCache 继承的自定义类时遇到错误。它应该下载图像并缓存它们;昨天,它工作正常,但今天早上我遇到了这个问题。

这是类的代码:

public class LruMemoryCache extends LruCache<String, Bitmap> {
private final Context context;
private static LruMemoryCache instance;

private LruMemoryCache(Context context) {
// super(1024 * 1024 * (((ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE)).getMemoryClass()) / 8);
super(5 * 1024 * 1024);
this.context = context;
}

public static synchronized LruMemoryCache getInstance(Context context) {
if (instance == null) {
instance = new LruMemoryCache(context);

}
return instance;
}

@Override
protected int sizeOf(String key, Bitmap value) {
return value.getByteCount();
}

public void loadBitmap(String url, ImageView imageView) {
final String imageKey = url;
final Bitmap bitmap = get(imageKey);
if (bitmap != null) {
imageView.setImageBitmap(bitmap);
} else {
BitmapWorkerTask task = new BitmapWorkerTask(imageView);
task.execute(url);
}
}

class BitmapWorkerTask extends AsyncTask<String, Void, Bitmap> {
Bitmap myBitmap;
ImageView mImageView;

public BitmapWorkerTask(ImageView imageView) {
mImageView = imageView;
}

@Override
protected Bitmap doInBackground(String... params) {
try {
URL url = new URL("http://www.google.com/logos/classicplus.png");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
myBitmap = BitmapFactory.decodeStream(input);
} catch (IOException e) {
e.printStackTrace();
}

put(params[0], myBitmap);
return myBitmap;
}

@Override
protected void onPostExecute(Bitmap result) {
super.onPostExecute(result);
mImageView.setImageBitmap(result);
}
}
}

这是来自 LogCat 的错误:

05-15 08:02:08.639: E/AndroidRuntime(12818): FATAL EXCEPTION: AsyncTask #2
05-15 08:02:08.639: E/AndroidRuntime(12818): java.lang.RuntimeException: An error occured while executing doInBackground()
05-15 08:02:08.639: E/AndroidRuntime(12818): at android.os.AsyncTask$3.done(AsyncTask.java:200)
05-15 08:02:08.639: E/AndroidRuntime(12818): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
05-15 08:02:08.639: E/AndroidRuntime(12818): at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
05-15 08:02:08.639: E/AndroidRuntime(12818): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
05-15 08:02:08.639: E/AndroidRuntime(12818): at java.util.concurrent.FutureTask.run(FutureTask.java:138)
05-15 08:02:08.639: E/AndroidRuntime(12818): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
05-15 08:02:08.639: E/AndroidRuntime(12818): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
05-15 08:02:08.639: E/AndroidRuntime(12818): at java.lang.Thread.run(Thread.java:1019)
05-15 08:02:08.639: E/AndroidRuntime(12818): Caused by: java.lang.NoSuchMethodError: getByteCount
05-15 08:02:08.639: E/AndroidRuntime(12818): at com.xs2theworld.sundio.caching.LruMemoryCache.sizeOf(LruMemoryCache.java:36)
05-15 08:02:08.639: E/AndroidRuntime(12818): at com.xs2theworld.sundio.caching.LruMemoryCache.sizeOf(LruMemoryCache.java:1)
05-15 08:02:08.639: E/AndroidRuntime(12818): at android.support.v4.util.LruCache.safeSizeOf(LruCache.java:230)
05-15 08:02:08.639: E/AndroidRuntime(12818): at android.support.v4.util.LruCache.put(LruCache.java:123)
05-15 08:02:08.639: E/AndroidRuntime(12818): at com.xs2theworld.sundio.caching.LruMemoryCache$BitmapWorkerTask.doInBackground(LruMemoryCache.java:72)
05-15 08:02:08.639: E/AndroidRuntime(12818): at com.xs2theworld.sundio.caching.LruMemoryCache$BitmapWorkerTask.doInBackground(LruMemoryCache.java:1)
05-15 08:02:08.639: E/AndroidRuntime(12818): at android.os.AsyncTask$2.call(AsyncTask.java:185)
05-15 08:02:08.639: E/AndroidRuntime(12818): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
05-15 08:02:08.639: E/AndroidRuntime(12818): ... 4 more

知道为什么会这样吗?

非常感谢大家。

编辑:我刚刚意识到这个错误不会发生在 Google Galaxy Nexus S 手机上,但它会发生在 Samsun Galaxy 上。有人知道为什么吗?

最佳答案

问题是由于:

@Override
protected int sizeOf(String key, Bitmap value) {
return value.getByteCount();
}

getByteCount() 方法只能与 API >= 12 一起使用,而 Samsung Galaxy 使用 API 10。

解决方法:

@Override
protected int sizeOf(String key, Bitmap value) {
if (Integer.valueOf(android.os.Build.VERSION.SDK_INT) >= 12)
return value.getByteCount();
else
return (value.getRowBytes() * value.getHeight());
}

关于android - NoSuchMethodError 在 Android 中使用 LruCache,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10596574/

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