UnsupportedOperationException: Failed to resolve attribute xxx系列的问题

接着上篇文章,讲到了有个UnsupportedOperationException的问题

Caused by: java.lang.reflect.InvocationTargetException\\n\\tat   
java.lang.reflect.Constructor.newInstance(Native Method)\\n\\tat   
com.elong.mobile.plugin.utils.PluginResInflateFactory.onCreateView(PluginResInflateFactory.java:45)\\n\\t... 38 more  
\\nCaused by: java.lang.UnsupportedOperationException: Failed to resolve attribute at index 5: TypedValue{t=0x2/d=0x1010351 a=1}\\n\\tat   
android.content.res.TypedArray.getColorStateList(TypedArray.java:493)\\n\\tat   
android.widget.TextView.<init>(TextView.java:1065)\\n\\tat   
android.widget.EditText.<init>(EditText.java:71)\\n\\tat   
android.widget.EditText.<init>(EditText.java:66)\\n\\tat  
android.widget.EditText.<init>(EditText.java:62)\\n\\t...   
40 more\\nandroid.view.InflateException: Binary XML file line #61: Binary XML file line #61: Error inflating class android.widget.EditText\\n\\tat   
android.view.LayoutInflater.inflate(LayoutInflater.java:539)\\n\\tat   
android.view.LayoutInflater.inflate(LayoutInflater.java:394)\\n\\tat   
com.elong.android.home.dialogutils.BaseHttpDialog.init(BaseHttpDialog.java:61)\\n\\tat   
com.elong.android.home.dialogutils.BaseHttpDialog.<init>(BaseHttpDialog.java:31)\\n\\tat   
com.elong.android.home.dialogutils.HttpVerifyCodeDialog.<init>(HttpVerifyCodeDialog.java:33)\\n\\tat   
com.elong.android.home.BaseNetFragment.initDialog(BaseNetFragment.java:44)\\n\\tat   
com.elong.android.home.RevisionHomeActivity.onCreateView(RevisionHomeActivity.java:246)\\n\\tat   
android.app.Fragment.performCreateView(Fragment.java:2236)\\n\\tat   
android.app.FragmentManagerImpl.moveToState(FragmentManager.java:978)\\n\\tat   
android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1153)\\n\\tat   
android.app.BackStackRecord.run(BackStackRecord.java:800)\\n\\tat   
android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1562)\\n\\tat   
android.app.FragmentController.execPendingActions(FragmentController.java:325)\\n\\tat   
android.app.Activity.performStart(Activity.java:6285)\\n\\tat   
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2510)\\n\\tat   
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2613)\\n\\tat   
android.app.ActivityThread.-wrap11(ActivityThread.java)\\n\\tat   
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1469)\\n\\tat   
android.os.Handler.dispatchMessage(Handler.java:111)\\n\\tat   
android.os.Looper.loop(Looper.java:207)\\n\\tat   
android.app.ActivityThread.main(ActivityThread.java:5692)\\n\\tat   
java.lang.reflect.Method.invoke(Native Method)\\n\\tat   
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:891)\\n\\tat   
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:752)\\n  
其中最重要的一行就是

java.lang.UnsupportedOperationException: Failed to resolve attribute at index 5: TypedValue{t=0x2/d=0x1010351 a=1}
这个错误是TypedArray代码抛出的

frameworks/base/core/java/android/content/res/TypedArray.java

    public ColorStateList getColorStateList(int index) {
        if (mRecycled) {
            throw new RuntimeException("Cannot make calls to a recycled instance!");
        }

        final TypedValue value = mValue;
        if (getValueAt(index*AssetManager.STYLE_NUM_ENTRIES, value)) {
            if (value.type == TypedValue.TYPE_ATTRIBUTE) {
                throw new UnsupportedOperationException(
                        "Failed to resolve attribute at index " + index + ": " + value);
            }
            return mResources.loadColorStateList(value, value.resourceId, mTheme);
        }
        return null;
    }
可以看出是TypedValue.TYPE_ATTRIBUTE类型就会抛出这个exception

TypeValue其它类型可参见代码,部分代码如下:

    /** The <var>data</var> field holds an attribute resource
     *  identifier (referencing an attribute in the current theme
     *  style, not a resource entry). */
    public static final int TYPE_ATTRIBUTE = 0x02;
    /** The <var>string</var> field holds string data.  In addition, if
     *  <var>data</var> is non-zero then it is the string block
     *  index of the string and <var>assetCookie</var> is the set of
     *  assets the string came from. */
    public static final int TYPE_STRING = 0x03;
    /** The <var>data</var> field holds an IEEE 754 floating point number. */
    public static final int TYPE_FLOAT = 0x04;
    /** The <var>data</var> field holds a complex number encoding a
     *  dimension value. */
    public static final int TYPE_DIMENSION = 0x05;
可以看出TypedValue{t=0x2/d=0x1010351 a=1}其中的t=0x2就是指类型为2,其它含义参见代码

    public String toString()
    {
        StringBuilder sb = new StringBuilder();
        sb.append("TypedValue{t=0x").append(Integer.toHexString(type));
        sb.append("/d=0x").append(Integer.toHexString(data));
        if (type == TYPE_STRING) {
            sb.append(" \"").append(string != null ? string : "<null>").append("\"");
        }
        if (assetCookie != 0) {
            sb.append(" a=").append(assetCookie);
        }
        if (resourceId != 0) {
            sb.append(" r=0x").append(Integer.toHexString(resourceId));
        }
        sb.append("}");
        return sb.toString();
    }
其中的"a="只对string有意义,这个获取的是color,所以“a=1”没啥含义
    /** Additional information about where the value came from; only
     *  set for strings. */
    public int assetCookie;
其中的d是资源id,framework中的资源id是预先指定好的,app的编译时动态确定。

frameworks/base/core/res/res/values/public.xml

  <public type="attr" name="editTextColor" id="0x01010351" />
其中的0x1010351就是指editTextColor,报错的原因就是editTextColor最终没有确认成一个确定的值。

原因从EditText的构造方法讲起

public EditText(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)
最后一个参数就是控件系统最后获取属性值的地方,默认是空的,但是控件同事指定了一个默认值,这个值是系统定义的一个值,并无修改,但是出错的原因就是这个。

参见frameworks/base/core/res/res/values中定义

    <style name="Widget.Material.EditText" parent="Widget.EditText"/>
    <style name="Widget.EditText">
        <item name="focusable">true</item>
        <item name="focusableInTouchMode">true</item>
        <item name="clickable">true</item>
        <item name="background">?attr/editTextBackground</item>
        <item name="textAppearance">?attr/textAppearanceMediumInverse</item>
        <item name="textColor">?attr/editTextColor</item>
        <item name="gravity">center_vertical</item>
        <item name="breakStrategy">simple</item>
        <item name="hyphenationFrequency">normal</item>
    </style>
可以看出textColor 的值就是attr,如果app的主题、app的xml代码、系统的默认attr都没有指定textColor或者editTextColor,那么当然最终会报错。

Android中xml中的属性定义>app主题>控件的第三个参数defStyleAttr>控件的第四个参数defStyleRes,如果四轮都没法确定一个属性的最终值,就会抛出错误。




  • 9
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值