【问题标题】:Support lib LruCache throws NullPointerException: key == null || value == null for some reason支持 lib LruCache 抛出 NullPointerException: key == null || value == null 出于某种原因
【发布时间】:2014-09-15 07:10:57
【问题描述】:

我一直在尝试了解如何使用 LruCache 来解决较低 API 中的 outOfMemory 错误问题,但是我在正确实现它时遇到了麻烦。

由于某种原因,当我尝试将 Drawable 和 url 字符串作为标识符放入缓存时,系统会抛出空指针异常:Key == null || value == null 错误,这对我来说是一种新的错误。我在互联网上搜索过,但似乎没有其他人遇到过这种错误。

关于我的实施中出了什么问题有什么想法吗?

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();
    }

}

}

【问题讨论】:

    标签: android image caching android-asynctask


    【解决方案1】:

    s_url 为空或图片为空。

    在添加到缓存之前检查 null:

    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);
    

    【讨论】:

    • 哇,我现在觉得自己很愚蠢。感谢您的帮助
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-06-05
    • 1970-01-01
    • 2021-02-05
    • 2015-08-24
    • 2011-03-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多