運行時沒問題 編譯打包時出現 Error: Expected resource of type styleable [ResourceType] 錯誤

錯誤提示

Error: Expected resource of type styleable [ResourceType] 

這個錯誤在編譯運行時候並不會出現,但是當需要編譯打包的時候,就會爆出這個異常。

這個錯誤出現的位置位於自定義View中,代碼如下:

TypedArray ta = mContext.obtainStyledAttributes(attrs);
boolean hasBottomLine = ta.getBoolean(0, false);
boolean hasTopLine = ta.getBoolean(1, false);

點擊異常信息會定位到第三行,只有當 TypedArray 獲取第二個屬性以後數據時,纔會出現此異常,ta.getBoolean(0, false) 這句則不會報錯,其實這應該是一個警告,所以纔會在調試的時候正常編譯,但卻在編譯簽名包的時候失敗。

解決辦法

解決辦法就是在使用 TypedArray 的方法處,加上 @SuppressWarnings(“ResourceType”) ,這樣即可過濾該警告,可以正常通過簽名編譯。例如:

@SuppressWarnings("ResourceType")
public void initView() {
    TypedArray ta = mContext.obtainStyledAttributes(attrs);
    boolean hasBottomLine = ta.getBoolean(0, false);
    boolean hasTopLine = ta.getBoolean(1, false);
    ta.recycle();
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章