getDrawable()可以抛出NullPointerException吗?

时间:2018-10-19 16:39:47

标签: java android nullpointerexception

我有一个扩展ImageView的类。在其中,我有一个调用getDrawable().getIntrinsicWidth();的函数。我大约有10万次安装,并且我有10条关于该行的报告,这些报告产生了nullpointerexception。我无法重现此问题,但仍想解决此问题。为此,我需要知道它会抛出什么,但是我无法对其进行测试。

我可以安全地假设getDrawable()不能引发nullpointer异常,但是由于某种原因返回null,因此getIntrinsicWidth()引发异常吗?

此外,为什么除非设置为null,否则它返回null?我仅通过ShareIntents设置图像,并按mimetyp“ image / *”进行过滤。如果Uri没有指向图像,在共享过程中会不会得到解决?还是我需要自己照顾?

1 个答案:

答案 0 :(得分:1)

检查Java文档中的ImageView.getDrawable()

/** Return the view's drawable, or null if no drawable has been
    assigned.
*/
public Drawable getDrawable() {
    if (mDrawable == mRecycleableBitmapDrawable) {
        // Consider our cached version dirty since app code now has a reference to it
        mRecycleableBitmapDrawable = null;
    }
    return mDrawable;
}

因此,它可以返回空指针。

这很有意义,因为如果您想清除ImageView,可以致电mImageView.setImageDrawable(null);