最近使用Android Studio開發一個新項目,剛做完幾個界面,跑在android 5.0上面很正常,因為都是挺簡單的布局,本以為應該不存在兼容性問題(Flag啊)。
偶然用了一個4.x的實機測試,發現杯具了,直接報錯退出了,發現log里面打出這么一句:
java.lang.UnsupportedOperationException: Can't convert to color: type=0x2
難以理解啊,沒辦法一步一步調試吧。
順便說一下調試要注意的問題,如果compileSdkVersion與實際出錯的android版本不符合,是沒法設置斷點的。
改為一致之后,一步一步看,最后發現問題出在一個drawable xml文件上,之前是這么寫的:
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <stroke android:width="0.5dp" android:color="?colorPrimary" /> </shape>
也是摸不着頭腦,很正常啊。嘗試一下吧?colorPrimary改成直接的color引用試一試吧。
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <stroke android:width="0.5dp" android:color="@color/carnation" /> </shape>
一運行,果然成功了。
看起來像是android4.x系統上因為某種原因,無法在drawable xml文件里面使用attr屬性,5.x上沒有這個問題。
希望對遇到類似
can't convert xxx type=0x00
問題的朋友有所啟發和幫助。
如果有人能夠告訴我具體原因,我也會十分感謝(果然android兼容是個大坑啊!