解决PhotoView的报错信息:ImageView no longer exists. You should not use this PhotoViewAttacher any more.

来源:互联网 发布:高级程序员php面试 编辑:程序博客网 时间:2024/05/16 02:28

PhotoView是一个易于使用的带缩放ImageView功能的第三方开源框架。项目地址(链接)


最近接收公司的多媒体项目,处理图片这个模块的时候,发现经常弹出以下异常信息:

ImageView no longer exists. You should not use this PhotoViewAttacher any more.

google了一下,发现第一页的搜索结果,给出的解决办法(链接)都是如下:

在PhotoViewAttacher 文件中 更改cleanup方法

// @SuppressWarnings("deprecation")// public final void cleanup() {// if (null != mImageView) {// mImageView.get().getViewTreeObserver().removeGlobalOnLayoutListener(this);// }// mViewTreeObserver = null;//// // Clear listeners too// mMatrixChangeListener = null;// mPhotoTapListener = null;// mViewTapListener = null;//// // Finally, clear ImageView// mImageView = null;// }@SuppressLint("NewApi")@SuppressWarnings("deprecation")public final void cleanup() {    if (VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN) {        if (null != mImageView) {            mImageView.get().getViewTreeObserver()                    .removeOnGlobalLayoutListener(this);        }        if (null != mViewTreeObserver && mViewTreeObserver.isAlive()) {            mViewTreeObserver.removeOnGlobalLayoutListener(this);            mViewTreeObserver = null;            // Clear listeners too            mMatrixChangeListener = null;            mPhotoTapListener = null;            mViewTapListener = null;            // Finally, clear ImageView            mImageView = null;        }    } else {        if (null != mImageView) {            mImageView.get().getViewTreeObserver()                    .removeGlobalOnLayoutListener(this);        }        if (null != mViewTreeObserver && mViewTreeObserver.isAlive()) {            mViewTreeObserver.removeGlobalOnLayoutListener(this);            mViewTreeObserver = null;            // Clear listeners too            mMatrixChangeListener = null;            mPhotoTapListener = null;            mViewTapListener = null;            // Finally, clear ImageView            mImageView = null;        }    }}

实践之后,发现问题依旧,遂此种方法是不可行的,自己只能在github源项目上看看有没有遇到相同问题的人,果不其然,还真给我找到了。
针对此问题,该开源项目的作者chrisbanes的原话(链接)是:

Change getImageView() to log an error rather than throw an exception.

大意更改getImageView()以log方式记录错误,而不是抛出异常。那么:

    public final ImageView getImageView() {    ImageView imageView = null;    if (null != mImageView) {        imageView = mImageView.get();    }    // If we don't have an ImageView, call cleanup()    if (null == imageView) {        cleanup();    //          throw new IllegalStateException(//此处经常抛异常,网上的解释是可能由于mImageView为弱引用的关系,对此,该开源框架PhotoView的创建人chrisbanes的建议是:Change getImageView() to log an error rather than throw an exception.    //                  "ImageView no longer exists. You should not use this PhotoViewAttacher any more.");        try {            throw new IllegalStateException(                    "ImageView no longer exists. You should not use this PhotoViewAttacher any more.");        } catch (Exception e) {            Log.w("PhotoViewAttacher", "[IllegalStateException] e = "+e.toString());        }    }    return imageView;}

按此方法更改后,再无此异常出现,欢迎大家指正

1 0
原创粉丝点击