Android 自定義ClassLoader加載插件出現(xiàn)的cannot be cast to異常問題

問題定義:

Android 使用自定義ClassLoader加載插件的時(shí)候,當(dāng)插件資源中使用自定義View后,會出現(xiàn)異常

java.lang.ClassCastException: android.support.v4.widget.SwipeRefreshLayout cannot be cast to android.support.v4.widget.SwipeRefreshLayout

</br>

問題分析:

同一個(gè)類的強(qiáng)制轉(zhuǎn)換異常,肯定是由于不同ClassLoader加載出現(xiàn)的問題,打印一下兩個(gè)ClassLoader,發(fā)現(xiàn)由布局文件解析出來的SwipeRefreshLayout是由PathClassLoader來加載,而不是由我們自定的的ClassLoader加載,應(yīng)該是解析XML的時(shí)候出現(xiàn)的問題,查看源碼中xml加載到View過程進(jìn)行分析。
從Activity的setContentView()方法進(jìn)入

public void setContentView(@LayoutRes int layoutResID) {
     getWindow().setContentView(layoutResID);
     initWindowDecorActionBar();
}

getWindow的mWindow對象是 com.android.internal.policy.PhoneWindow,查看PhoneWindow的setContentView()方法

public void setContentView(int layoutResID) {
        ......
        if (hasFeature(FEATURE_CONTENT_TRANSITIONS)) {
            final Scene newScene = Scene.getSceneForLayout(mContentParent, layoutResID,
                    getContext());
            transitionTo(newScene);
        } else {
            mLayoutInflater.inflate(layoutResID, mContentParent);
        }
        ......
    }

XML的解析是使用mLayoutInflater的inflate方法來創(chuàng)建View,一直往下找,發(fā)現(xiàn)自定義的View是由createView()方法創(chuàng)建,查看該方法

 View createViewFromTag(View parent, String name, Context context, AttributeSet attrs,
            boolean ignoreThemeAttr) {
                try {
                    if (-1 == name.indexOf('.')) {
                        view = onCreateView(parent, name, attrs); //補(bǔ)全 android.view. ,最終還是指向createView
                    } else {
                        view = createView(name, null, attrs);
                    }
                } finally {
                    mConstructorArgs[0] = lastContext;
                }
}

private static final HashMap<String, Constructor<? extends View>> sConstructorMap =
            new HashMap<String, Constructor<? extends View>>();
public final View createView(String name, String prefix, AttributeSet attrs)
            throws ClassNotFoundException, InflateException {
        Constructor<? extends View> constructor = sConstructorMap.get(name);
        try {
            if (constructor == null) {
                //使用ClassLoader進(jìn)行加載
                clazz = mContext.getClassLoader().loadClass(
                        prefix != null ? (prefix + name) : name).asSubclass(View.class);
                constructor = clazz.getConstructor(mConstructorSignature);
                constructor.setAccessible(true);
                //添加到靜態(tài)緩存
                sConstructorMap.put(name, constructor);
            } else {  
            } 
            final View view = constructor.newInstance(args);
            return view;
        }  
    }

發(fā)現(xiàn)每個(gè)View在創(chuàng)建之前,都會從靜態(tài)sConstructorMap中獲取以前創(chuàng)建添加的緩存。
那么如果插件中使用到了宿主里面已經(jīng)創(chuàng)建過的自定義View,那么進(jìn)行類型轉(zhuǎn)化的時(shí)候就會出現(xiàn) cannot be cast to 的異常。
</br>

解決方案:

這個(gè)問題可以看成 在同一個(gè)進(jìn)程中,同一個(gè)自定義View對象,是無法用不同ClassLoader加載的;
那么,不同的進(jìn)程對靜態(tài)變量是不共享的,所以可以對插件的活動(dòng)指定不同的進(jìn)程。通過對中間Activity和Service指定process來解決該問題。

android:process=":PluginProcess"

</br>

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 178,741評論 25 709
  • 在問題的最右處對問題進(jìn)行等級評定,層層深入 1.Android線程之間的異步通信有哪些?(C) handler.s...
    super_shanks閱讀 800評論 0 3
  • 介紹自己負(fù)責(zé)的部分,如何實(shí)現(xiàn)的。 自定義view viewGroup activity的啟動(dòng)流程 事件傳遞及滑動(dòng)沖...
    東經(jīng)315度閱讀 1,345評論 1 4
  • 孩子的腳燙傷了一個(gè)多星期,這一個(gè)多星期都是的背著上學(xué) ,今天去輔導(dǎo)班接他,背起他,,一百斤的媽,背起七十多斤的孩,...
    映卿閱讀 171評論 0 0
  • 題目: Given a positive integer n, find the least number of ...
    八刀一閃閱讀 526評論 0 0

友情鏈接更多精彩內(nèi)容