本文鏈接:【Android】Error: Expected resource of type styleable [ResourceType] - 祝福的博客 - 博客頻道 - CSDN.NET
錯誤提示:
Error: Expected resource of type styleable [ResourceType]
這個錯誤在編譯運行時候并不會出現(xiàn),但是當需要編譯打包的時候,就會爆出這個異常。
這個錯誤出現(xiàn)的位置位于自定義View中,代碼如下:
TypedArray ta = mContext.obtainStyledAttributes(attrs);
? ? ?booleanhasBottomLine = ta.getBoolean(0,false);
? ? ?booleanhasTopLine = ta.getBoolean(1,false);
點擊異常信息會定位到第三行,只有當 TypedArray 獲取第二個屬性以后數(shù)據(jù)時,才會出現(xiàn)此異常,ta.getBoolean(0, false) 這句則不會報錯,其實這應該是一個警告,所以才會在調(diào)試的時候正常編譯,但卻在編譯簽名包的時候失敗。
解決辦法就是在使用TypedArray的方法處,加上@SuppressWarnings("ResourceType"),這樣即可過濾該警告,可以正常通過簽名編譯。例如:
@SuppressWarnings("ResourceType")
publicvoidinitView() {? ?
? ? TypedArray ta = ?mContext.obtainStyledAttributes(attrs);
? ? booleanhasBottomLine = ta.getBoolean(0,false);
? ? booleanhasTopLine = ta.getBoolean(1,false);? ?
? ? ta.recycle();}