首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >EditText addTextChangedListener导致内存泄漏

EditText addTextChangedListener导致内存泄漏
EN

Stack Overflow用户
提问于 2015-05-22 04:00:36
回答 1查看 2.1K关注 0票数 1

我使用LeakCanary检查我的应用程序是否有内存泄漏,它报告的泄漏情况如下

代码语言:javascript
复制
 public AutofitHelper setEnabled(boolean enabled) {
    if (mEnabled != enabled) {
        mEnabled = enabled;

        if (enabled) {
            mTextView.addTextChangedListener(mTextWatcher);
            mTextView.addOnLayoutChangeListener(mOnLayoutChangeListener);

            autofit();
        } else {
        android.util.Log.i("linlian","AutofitHelper.setEnabled()remove="+mTextView);
            mTextView.removeTextChangedListener(mTextWatcher);
            mTextView.removeOnLayoutChangeListener(mOnLayoutChangeListener);

            mTextView.setTextSize(TypedValue.COMPLEX_UNIT_PX, mTextSize);
        }
    }
    return this;
}

代码调用setEnable(true)为TextView添加addTextChangedListener,我已经将setEnable(false)添加到removeTextChangedListener中,但这还不够,还有一个静态TextLine.sCached引用,即如何释放sCashed

下面是我在TextLine中找到的代码片段

代码语言:javascript
复制
static TextLine recycle(TextLine tl) {
    tl.mText = null;
    tl.mPaint = null;
    tl.mDirections = null;

    tl.mMetricAffectingSpanSpanSet.recycle();
    tl.mCharacterStyleSpanSet.recycle();
    tl.mReplacementSpanSpanSet.recycle();

    synchronized(sCached) {
        for (int i = 0; i < sCached.length; ++i) {
            if (sCached[i] == null) {
                sCached[i] = tl;
                break;
            }
        }
    }
    return null;
}

但是,如何正确地使用它来循环静态sCashed

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-05-28 06:25:42

我找到了一个相关的话题文本视图上的Android内存泄漏

Utils.clearTextLineCache()也许是一个很好的解决方案。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30388139

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档