支持LIB LruCache抛出NullPointerException异常:关键== NULL ||值== NULL因为某些原因 [英] Support lib LruCache throws NullPointerException: key == null || value == null for some reason

查看:177
本文介绍了支持LIB LruCache抛出NullPointerException异常:关键== NULL ||值== NULL因为某些原因的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在试图了解如何使用LruCache打击内存不足的错误的问题,在较低的API,但是我一直有正确实现它的麻烦。

I've been trying to understand how to use the LruCache to combat the problem of outOfMemory errors in the lower APIs however I've been having trouble implementing it correctly.

由于某些原因,当我试图把一个可绘制和URL字符串在高速缓存作为标识符系统抛出一个NullPointerException异常:主要== NULL ||值== NULL错误,是一种新型的错误给我。我已经搜索在互联网,但似乎没有人有过这样那样的错误。

For some reason when I try to put a Drawable and url string in cache as an identifier the system throws a nullpointerexception: Key == null || value == null error which is a new kind of error to me. I have searched around the internet but it seems no one else has had this kind of error.

这是什么任何想法是怎么了?我在执行?

Any Ideas on what is going wrong in my implementation?

public class RedditIconTask {
private static final String debugTag = "ImageWorker";

private HashMap<String, Drawable> imageCache;
private LruCache<String,Drawable> imgCache;
private static Drawable DEFAULT_ICON = null;
private BaseAdapter adapter;
private Boolean cancelled = false;


public RedditIconTask (Context context)
{
    final int maxMem = (int)(Runtime.getRuntime().maxMemory()/1024);
    final int cacheSize = maxMem/8;
    imgCache = new LruCache<String, Drawable>(cacheSize);
    //sets faux-image cache in form of HashMap stores drawables in memory
    imageCache = new HashMap<String, Drawable>();

}

public Drawable loadImage (BaseAdapter adapt, ImageView view)
{
    //checks if image is in memory and makes a call to Reddit Icon task if imaage must be downloaded again.
    this.adapter = adapt;
    String url = (String) view.getTag();
    if (imgCache.get(url)!= null)
    {
        return getDrawableFromMemCache(url);
    }
    else {
        new ImageTask().execute(url);
        return DEFAULT_ICON;
    }
}
//receives cancel async task request from MainFragment on Pause
public void stopImage (Boolean stop){
        cancelled=stop;
    Log.v(debugTag,"Stop AsyncTask");

}
//sets state of cancelled variable
public boolean cancelled (){
    return cancelled;

}
public Drawable getDrawableFromMemCache(String key) {
    return  imgCache.get(key);
}

public class ImageTask extends AsyncTask<String, Void, Drawable>
{
    private String s_url;

    //accepts array of urls to down load
    @Override
    protected Drawable doInBackground(String... params) {
        //checks the cancelled variable to determine whether to continue AsyncTask
       if (cancelled()){
           cancel(cancelled);
        //checks urls for drawable types
       }else {
           s_url = params[0];
           InputStream inStream;
           Drawable picture = null;
           try {
               Log.v(debugTag, "Fetching: " + s_url);


               URL url = new URL(s_url);
               inStream = url.openStream();
               picture = Drawable.createFromStream(inStream, "src");

           } catch (MalformedURLException e) {
               Log.v(debugTag, "Malformed: " + e.getMessage());
           } catch (IOException e) {
               Log.d(debugTag, "I/O : " + e.getMessage());

           }

           imgCache.put(s_url, picture);
           return picture;
       }
        return null;
    }

    @Override
    protected void onPostExecute(Drawable result) {
        super.onPostExecute(result);

            //adds resulting drawable to memory



        //updates adapter view
        adapter.notifyDataSetChanged();
    }

}

}

推荐答案

S_URL为空或图片为null。

s_url is null or picture is null.

添加到高速缓存之前检查是否为空:

Check for null before adding to cache:

if(s_url!=null && picture!=null)
    imgCache.put(s_url, picture);
else
    Log.d(debugTag, "Put in cache failed for url:"+s_url+" and pict:"+picture);

这篇关于支持LIB LruCache抛出NullPointerException异常:关键== NULL ||值== NULL因为某些原因的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆