java.lang.UnsupportedOperationException:无法转换为color:type = 0x2 on setErrorEnabled for InputTextLayout

时间:2016-03-16 23:36:55

标签: java android android-textinputlayout

我有一个TextInputLayout:

<android.support.design.widget.TextInputLayout
    android:id="@+id/passwordLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textColorHint="#FFF"
    android:theme="@style/TextInputLayoutWhite"
    app:errorTextAppearance="@style/ErrorText"
    app:hintTextAppearance="@style/TextInputLayoutWhite">

    <EditText
        android:id="@+id/passwordInput"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Password"
        android:imeOptions="actionGo"
        android:singleLine="true"
        android:textColor="#FFF"
        app:theme="@style/WhiteEditText">

    </EditText>
</android.support.design.widget.TextInputLayout>

我在我的Activity中设置错误就像这样:

passwordLayout.setErrorEnabled(true);

这就是抛出java.lang.UnsupportedOperationException: Can't convert to color: type=0x2

如果我在xml布局中的TextInputLayout上设置app:errorEnabled,则会显示错误文本,但我不希望它始终“启用”,因为它为textview添加了额外的空间,这是不必要的直到我需要显示错误。

为什么会抛出java.lang.UnsupportedOperationException的任何想法?这是完整的堆栈跟踪:

03-16 19:31:22.078 32577-32577/com.example.myapp W/ResourceType: Too many attribute references, stopped at: 0x01010099
03-16 19:31:22.078 32577-32577/com.example.myapp W/ResourceType: Too many attribute references, stopped at: 0x0101009a
03-16 19:31:22.078 32577-32577/com.example.myapp W/ResourceType: Too many attribute references, stopped at: 0x0101009b
03-16 19:31:22.078 32577-32577/com.example.myapp W/System.err: java.lang.UnsupportedOperationException: Can't convert to color: type=0x2
03-16 19:31:22.086 32577-32577/com.example.myapp W/System.err:     at android.content.res.TypedArray.getColor(TypedArray.java:326)
03-16 19:31:22.086 32577-32577/com.example.myapp W/System.err:     at android.widget.TextView.<init>(TextView.java:635)
03-16 19:31:22.086 32577-32577/com.example.myapp W/System.err:     at android.widget.TextView.<init>(TextView.java:578)
03-16 19:31:22.086 32577-32577/com.example.myapp W/System.err:     at android.widget.TextView.<init>(TextView.java:574)
03-16 19:31:22.086 32577-32577/com.example.myapp W/System.err:     at android.support.design.widget.TextInputLayout.setErrorEnabled(TextInputLayout.java:380)
03-16 19:31:22.086 32577-32577/com.example.myapp W/System.err:     at com.example.myapp.fragment.LoginFragment.onLoginFailure(LoginFragment.java:86)
03-16 19:31:22.086 32577-32577/com.example.myapp W/System.err:     at com.example.myapp.service.AccountService$1.onResponse(AccountService.java:31)
03-16 19:31:22.094 32577-32577/com.example.myapp W/System.err:     at com.example.myapp.service.AccountService$1.onResponse(AccountService.java:25)
03-16 19:31:22.094 32577-32577/com.example.myapp W/System.err:     at com.android.volley.toolbox.JsonRequest.deliverResponse(JsonRequest.java:65)
03-16 19:31:22.094 32577-32577/com.example.myapp W/System.err:     at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99)
03-16 19:31:22.094 32577-32577/com.example.myapp W/System.err:     at android.os.Handler.handleCallback(Handler.java:725)
03-16 19:31:22.094 32577-32577/com.example.myapp W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:92)
03-16 19:31:22.094 32577-32577/com.example.myapp W/System.err:     at android.os.Looper.loop(Looper.java:137)
03-16 19:31:22.094 32577-32577/com.example.myapp W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5041)
03-16 19:31:22.094 32577-32577/com.example.myapp W/System.err:     at java.lang.reflect.Method.invokeNative(Native Method)
03-16 19:31:22.094 32577-32577/com.example.myapp W/System.err:     at java.lang.reflect.Method.invoke(Method.java:511)
03-16 19:31:22.094 32577-32577/com.example.myapp W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-16 19:31:22.094 32577-32577/com.example.myapp W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-16 19:31:22.094 32577-32577/com.example.myapp W/System.err:     at dalvik.system.NativeStart.main(Native Method)

更新

这是ErrorText风格的内容。请注意,即使我没有设置主题,也会抛出上述错误:

<style name="ErrorText" parent="TextAppearance.AppCompat.Small">
    <item name="android:textColor">#FFF</item>
</style>

这是我的密码声明:

TextInputLayout passwordLayout = (TextInputLayout) view.findViewById(R.id.passwordLayout);

1 个答案:

答案 0 :(得分:7)

this answer表明您的android:theme="@style/TextLabelWhite"存在问题。删除此项或将样式应用于您的父AppTheme。