在Android中使用LruCache时出现NoSuchMethodError
我在使用从Android中的LruCache继承的自定义类时出错。 它应该下载图像并缓存它们;昨天,它还在工作,但今天早上我遇到了这个问题 这是该类的代码:在Android中使用LruCache时出现NoSuchMethodError,android,caching,android-lru-cache,Android,Caching,Android Lru Cache,我在使用从Android中的LruCache继承的自定义类时出错。 它应该下载图像并缓存它们;昨天,它还在工作,但今天早上我遇到了这个问题 这是该类的代码: public class LruMemoryCache extends LruCache<String, Bitmap> { private final Context context; private static LruMemoryCache instance; private LruMemoryC
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);
}
}
}
知道为什么会这样吗
谢谢大家
编辑:我刚刚意识到谷歌Galaxy Nexus S手机不会出现这种错误,但Samsun Galaxy手机会出现这种错误。。有人知道原因吗?问题是由以下原因引起的:
@Override
protected int sizeOf(String key, Bitmap value) {
return value.getByteCount();
}
getByteCount()方法只能与API>=12一起使用,三星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());
}
对于LruCache,请使用google的支持库,或获取其原始源代码
通常,理解他们在源代码中所做的事情是非常复杂的,但这一次非常简单。我尝试了上述所有方法,它们都很接近,但并不完全正确(至少在我的情况下) 我正在使用bitmap.getByteCount();创建新LruCache时,在sizeOf()方法内部:
mMemoryCache = new LruCache<String, Bitmap>(cacheSize) {
@Override
protected int sizeOf(String key, Bitmap bitmap) {
return bitmap.getByteCount();
}
};
这很好,但我注意到返回的值不同,当我使用上面的建议时,它甚至不会在我的设备上进行缓存。我在运行api 3.2的Nexus One和运行4.2的Galaxy Nexus上测试了返回值:
bitmap.getByteCount(); returned-> 15
bitmap.getRowBytes() * bitmap.getHeight(); returned-> 15400
因此,为了解决我的问题,我只做了以下几点:
return (bitmap.getRowBytes() * bitmap.getHeight()) / 1000;
而不是:
return bitmap.getByteCount();
可能与您所处的情况不同,但这对我很有效。我明白了,所以您应该使用android的代码。经过几次调整(更改map.eldest()部分),这里有一些对我有用的东西:@androiddeveloper你能告诉我你是如何摆脱由
map.eldest()生成的错误的吗?
当然。重写LinkedHashMap的RemoveedStantry(),或者使用cacheMap.keySet().iterator().next(),这将返回最长的键。然后您可以获取它的值(如果需要)并从LinkedHashMap中删除该键。@androiddeveloper我已经替换了Map.Entry toEvict=Map.eldest()
byMap.Entry toEvict=(Entry)Map.keySet().iterator().next()代码>我希望这就足够了?当我在getByteCount()
@Hunt上仍然遇到编译时错误时,你是如何解决这个问题的?检查我下面的评论,接受的答案无论我在哪里看,我读到的每一个源代码,我看到的所有内容返回(bitmap.getRowBytes()*bitmap.getHeight())/1000代码>。它必须是我正在保存的文件类型或其他类型。所以下次我需要使用bitmap.getByteCount()代码>,我将先做一些测试,看看我需要什么值。
return (bitmap.getRowBytes() * bitmap.getHeight()) / 1000;
return bitmap.getByteCount();