Android 獲取 Activity 時遇到 TintContextWrapper cannot be cast to 的問題

問題描述:

若是一個 View 繪製於某個 Activity 的 ContentView 上, 那它的 Context 必定是和這個 Activity 相關聯的. 所以咱們想在 View 中直接用 Activity 方法時 (最經常使用的應該就是 Activity.startActivity() 方法了), 沒必要再向 View 中傳遞 Activity 對象.
通常在 View 中獲取這個 Activity 對象都是簡單的用下面代碼就能夠了:java

Activity activity = (Activity) getContext();

但在 View 繼承自 AppCompat 系的 View 時 (好比 AppCompatTextView, AppCompatImageView), 上面方法可能會獲得下面異常:android

**java.lang.ClassCastException: android.support.v7.widget.TintContextWrapper cannot be cast to ...Activity**

問題緣由:

緣由其實很簡單. 上 AppCompatTextView 的構造函數代碼:app

public class AppCompatTextView extends TextView implements TintableBackgroundView {

    ......

    public AppCompatTextView(Context context) {
        this(context, null);
    }

    public AppCompatTextView(Context context, AttributeSet attrs) {
        this(context, attrs, android.R.attr.textViewStyle);
    }

    public AppCompatTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(TintContextWrapper.wrap(context), attrs, defStyleAttr);  // 注意這行

        mBackgroundTintHelper = new AppCompatBackgroundHelper(this);
        mBackgroundTintHelper.loadFromAttributes(attrs, defStyleAttr);

        mTextHelper = AppCompatTextHelper.create(this);
        mTextHelper.loadFromAttributes(attrs, defStyleAttr);
        mTextHelper.applyCompoundDrawablesTints();
    }

    ......
}

在構造函數中, context 被用 TintContextWrapper 包了一層. 因此這時 Activity 其實被保存在了 TintContextWrapper 中的 BaseContext 中了.
TintContextWrapper 不能直接強制轉換爲 Activity.函數

網上有說 23.3.0 的 v7 以後的包中會有這個問題, 以前的不會. 我手頭只有 25.3.0 的源碼, 沒有驗證其餘版本的代碼是什麼樣子. 不過這不重要, 由於下面的解決方案能夠兼容各個狀況.

問題解決:

知道緣由就簡單了. 能夠簡單的用 TintContextWrapper.getBaseContext() 獲得這個 Activity.
但其實一層層的從 ContextWrapper 中把 Activity 剝出來更保險:this

/**
     * try get host activity from view.
     * views hosted on floating window like dialog and toast will sure return null.
     * @return host activity; or null if not available
     */
    public static Activity getActivityFromView(View view) {
        Context context = view.getContext();
        while (context instanceof ContextWrapper) {
            if (context instanceof Activity) {
                return (Activity) context;
            }
            context = ((ContextWrapper) context).getBaseContext();
        }
        return null;
    }

如上方法能夠通用的解決從 View 中獲取 Activity 的問題.code

事實上, 谷歌 v7 包中的 android.support.v7.app.MediaRouteButton 就是這麼幹的.對象

參考:

Android get hosting Activity from a view繼承

相關文章
相關標籤/搜索