为什么他们的WeakReference检查为空? [英] Why do they check WeakReference for null?

查看:334
本文介绍了为什么他们的WeakReference检查为空?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是Android开发者博客文章就如何异步下载图片:
http://android-developers.blogspot.de/2010/07/multithreading-for-performance.html

Here is the blog post on android developers on how download images asynchronously: http://android-developers.blogspot.de/2010/07/multithreading-for-performance.html

这是它的code片断:

The code snippet from it:

class BitmapDownloaderTask extends AsyncTask<String, Void, Bitmap> {
    private String url;
    private final WeakReference<ImageView> imageViewReference;

    public BitmapDownloaderTask(ImageView imageView) {
        imageViewReference = new WeakReference<ImageView>(imageView);
    }

    @Override
    // Actual download method, run in the task thread
    protected Bitmap doInBackground(String... params) {
         // params comes from the execute() call: params[0] is the url.
         return downloadBitmap(params[0]);
    }

    @Override
    // Once the image is downloaded, associates it to the imageView
    protected void onPostExecute(Bitmap bitmap) {
        if (isCancelled()) {
            bitmap = null;
        }

        if (imageViewReference != null) {
            ImageView imageView = imageViewReference.get();
            if (imageView != null) {
                imageView.setImageBitmap(bitmap);
            }
        }
    }
}

问题:
为什么他们检查imageViewReference为空?那是一个拼写错误?

Question: Why do they check imageViewReference for null? Is that an misspell?

推荐答案

看起来像真的过时code。删除此检查,没什么不好应该发生。

Looks like really stale code. Remove this check, nothing bad should happen.

这篇关于为什么他们的WeakReference检查为空?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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