简体   繁体   English

他们为什么检查WeakReference是否为null?

[英]Why do they check WeakReference for null?

Here is the blog post on android developers on how download images asynchronously: http://android-developers.blogspot.de/2010/07/multithreading-for-performance.html 这是有关android开发人员的博客文章,介绍如何异步下载图像: http : //android-developers.blogspot.de/2010/07/multithreading-for-performance.html

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

Question: Why do they check imageViewReference for null? 问题:为什么他们要检查imageViewReference是否为null? Is that an misspell? 那是拼写错误吗?

Looks like really stale code. 看起来真的很陈旧。 Remove this check, nothing bad should happen. 删除此检查,不会发生任何不良情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我需要检查AsyncTask中的WeakReference吗? - Do I need to check a WeakReference inside an AsyncTask? 为什么我的WeakReference在我的AsyncTask中得到NULL? - Why does my WeakReference gets NULL inside my AsyncTask? 弱引用在异步任务中为null - Weakreference get null in async task 为什么从WeakReference.get()获得的类的非null类型属性更改为nullable - Why are non-null type properties of class obtained from WeakReference.get() changing to nullable 为什么在android Listeners上使用WeakReference? - why use WeakReference on android Listeners? 首先为什么我会在 Rxjava2 和 Retrofit 中出现这个错误? 第二 我应该直接使用 Wea​​kReference 还是 Activity? - FIRST Why do I get this errorwith Rxjava2 and Retrofit? SECOND Should I be using WeakReference or Activity directly? 为什么在添加片段时检查savedInstanceState == null? - Why do you check for savedInstanceState == null when adding fragment? 对函数类型参数null行为的弱引用 - WeakReference to function type parameter null behavior 为什么Leakcanary声明弱引用导致泄漏? - Why does Leakcanary state that a weakreference caused a leak? 为什么对服务对象有静态弱引用? - Why have a static weakreference to service object?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM