在Fragment中设置错误的TextInputLayout[英] set error TextInputLayout in Fragment

本文是小编为大家收集整理的关于在Fragment中设置错误的TextInputLayout的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我在设置错误消息时遇到了 TextInputLayout 问题.我在片段中使用以下包'com.android.support:design:23.1.1'.这是代码

if (tvName.getText().toString().trim().isEmpty()) {
        textInputLayoutName.setErrorEnabled(true);
        textInputLayoutName.setError("erreur");
    }

XML 部分

       <android.support.design.widget.TextInputLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/TextLabel"
            android:id="@+id/input_layout_name">
            <EditText
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:id="@+id/name"
                android:hint="@string/name"/>
        </android.support.design.widget.TextInputLayout>

风格

 <style name="TextLabel" parent="TextAppearance.AppCompat">
    <item name="android:textColorHint">@android:color/darker_gray</item>
    <item name="android:textSize">20sp</item>
    <item name="colorAccent">@color/colorPrimary</item>
    <item name="colorControlNormal">#2E7D32</item>
    <item name="colorControlActivated">#1B5E20</item>
</style>

这里是错误

   FATAL EXCEPTION: main
   Process: com.example.adama.findmypharmacie, PID: 21169
   java.lang.UnsupportedOperationException: Can t convert to color: type=0x2
   at android.content.res.TypedArray.getColor(TypedArray.java:327)
   at android.widget.TextView.<init>(TextView.java:695)
   at android.widget.TextView.<init>(TextView.java:637)
   at android.widget.TextView.<init>(TextView.java:633)
   at android.support.design.widget.TextInputLayout.setErrorEnabled(TextInputLayout.java:380) 
   at android.support.design.widget.TextInputLayout.setError(TextInputLayout.java:425)
   at com.example.adama.findmypharmacie.fragments.MainFragment.onClick(MainFragment.java:185)
   at android.view.View.performClick(View.java:4463)
   at android.view.View$PerformClick.run(View.java:18789)
   at android.os.Handler.handleCallback(Handler.java:808)
   at android.os.Handler.dispatchMessage(Handler.java:103)
   at android.os.Looper.loop(Looper.java:193)
   at android.app.ActivityThread.main(ActivityThread.java:5299)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:515)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:829)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:645)
   at dalvik.system.NativeStart.main(Native Method)

推荐答案

由于您正在对布局进行主题化,因此不应从 TextAppearance 派生.相反,从 ThemeOverlay 派生它.例如:

<style name="TextLabel" parent="ThemeOverlay.AppCompat.Light">
    <item name="android:textColorHint">@android:color/darker_gray</item>
    <item name="android:textSize">20sp</item>
    <item name="colorAccent">@color/colorPrimary</item>
    <item name="colorControlNormal">#2E7D32</item>
    <item name="colorControlActivated">#1B5E20</item>
</style>

本文地址:https://www.itbaoku.cn/post/2091079.html

问题描述

I have an issue with TextInputLayout when I set the error message. I'm using the following package 'com.android.support:design:23.1.1' in a Fragment. This is the code

if (tvName.getText().toString().trim().isEmpty()) {
        textInputLayoutName.setErrorEnabled(true);
        textInputLayoutName.setError("erreur");
    }

the XML part

       <android.support.design.widget.TextInputLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/TextLabel"
            android:id="@+id/input_layout_name">
            <EditText
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:id="@+id/name"
                android:hint="@string/name"/>
        </android.support.design.widget.TextInputLayout>

The style

 <style name="TextLabel" parent="TextAppearance.AppCompat">
    <item name="android:textColorHint">@android:color/darker_gray</item>
    <item name="android:textSize">20sp</item>
    <item name="colorAccent">@color/colorPrimary</item>
    <item name="colorControlNormal">#2E7D32</item>
    <item name="colorControlActivated">#1B5E20</item>
</style>

and here the error

   FATAL EXCEPTION: main
   Process: com.example.adama.findmypharmacie, PID: 21169
   java.lang.UnsupportedOperationException: Can t convert to color: type=0x2
   at android.content.res.TypedArray.getColor(TypedArray.java:327)
   at android.widget.TextView.<init>(TextView.java:695)
   at android.widget.TextView.<init>(TextView.java:637)
   at android.widget.TextView.<init>(TextView.java:633)
   at android.support.design.widget.TextInputLayout.setErrorEnabled(TextInputLayout.java:380) 
   at android.support.design.widget.TextInputLayout.setError(TextInputLayout.java:425)
   at com.example.adama.findmypharmacie.fragments.MainFragment.onClick(MainFragment.java:185)
   at android.view.View.performClick(View.java:4463)
   at android.view.View$PerformClick.run(View.java:18789)
   at android.os.Handler.handleCallback(Handler.java:808)
   at android.os.Handler.dispatchMessage(Handler.java:103)
   at android.os.Looper.loop(Looper.java:193)
   at android.app.ActivityThread.main(ActivityThread.java:5299)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:515)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:829)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:645)
   at dalvik.system.NativeStart.main(Native Method)

推荐答案

Since you are theming a layout you should not derived from TextAppearance. Instead, derive it from ThemeOverlay. For example:

<style name="TextLabel" parent="ThemeOverlay.AppCompat.Light">
    <item name="android:textColorHint">@android:color/darker_gray</item>
    <item name="android:textSize">20sp</item>
    <item name="colorAccent">@color/colorPrimary</item>
    <item name="colorControlNormal">#2E7D32</item>
    <item name="colorControlActivated">#1B5E20</item>
</style>