为什么他们检查WeakReference为null?

时间:2014-01-24 15:36:52

标签: android multithreading image android-asynctask weak-references

以下是Android开发人员关于如何异步下载图像的博客文章: http://android-developers.blogspot.de/2010/07/multithreading-for-performance.html

来自它的代码段:

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为null?那是拼错吗?

1 个答案:

答案 0 :(得分:0)

看起来真的是陈旧的代码。删除此检查,不会发生任何不好的事情。