Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/210.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android TextInputLayout setError方法在24.2.0中抛出ClassCastException_Android_Android Design Library_Android Textinputlayout - Fatal编程技术网

Android TextInputLayout setError方法在24.2.0中抛出ClassCastException

Android TextInputLayout setError方法在24.2.0中抛出ClassCastException,android,android-design-library,android-textinputlayout,Android,Android Design Library,Android Textinputlayout,我将支持库版本更新为24.2.0,我的注册屏幕现在已关闭。问题在于TextInputLayout,我有两种方法: protected void setError(@Nullable CharSequence errorMsg, @NonNull EditText editText, boolean forceClean) { TextInputLayout viewParent = (TextInputLayout) editText.getParent();

我将支持库版本更新为24.2.0,我的注册屏幕现在已关闭。问题在于TextInputLayout,我有两种方法:

    protected void setError(@Nullable CharSequence errorMsg, @NonNull EditText editText, boolean forceClean) {
        TextInputLayout viewParent = (TextInputLayout) editText.getParent();
        if (forceClean) {
            viewParent.setErrorEnabled(false);
            viewParent.setError(null);
        }
        viewParent.setErrorEnabled(true);
        viewParent.setError(errorMsg);
    }

    protected void clearError(@NonNull EditText editText) {
        TextInputLayout viewParent = (TextInputLayout) editText.getParent();
        viewParent.setErrorEnabled(false);
        viewParent.setError(null);
    }
当我试图将EditText的父级强制转换为TextInputLayout时,我遇到了一个错误,在布局中,我有这样的代码:

<android.support.design.widget.TextInputLayout
     android:layout_width="match_parent"
     android:layout_height="wrap_content">

     <android.support.design.widget.TextInputEditText
          android:id="@+id/login_registration_firstname"
          style="@style/registration_form_field"
          android:hint="@string/login_registration_firstname"
          android:inputType="textCapWords" />
</android.support.design.widget.TextInputLayout>

我想知道这方面是否有一些新的指南?

问题调查显示,出于某种原因,存在额外的布局,因此现在我们有了TextInputLayout->FrameLayout->TextInputItemText,这很令人伤心:(作为临时解决方法,我创建了以下方法:

@Nullable
private TextInputLayout getTextInputLayout(@NonNull EditText editText) {
        View currentView = editText;
        for (int i = 0; i < 2; i++) {
            ViewParent parent = currentView.getParent();
            if (parent instanceof TextInputLayout) {
                return (TextInputLayout) parent;
            } else {
                currentView = (View) parent;
            }
        }
        return null;
}
@Nullable
私有TextInputLayout GettInputLayout(@NonNull EditText EditText){
查看当前视图=编辑文本;
对于(int i=0;i<2;i++){
ViewParent=currentView.getParent();
if(TextInputLayout的父实例){
返回(TextInputLayout)父级;
}否则{
currentView=(视图)父级;
}
}
返回null;
}
这个将在视图层次结构中找到TextInputLayout

如果幸运的话,您会注意到粉红色错误颜色的变化,克服它的简单方法是覆盖styles.xml中的样式:

<style name="TextAppearance.Design.Error" parent="TextAppearance.AppCompat.Caption" tools:override="true">
        <item name="android:textColor">@color/red</item>
</style>

@颜色/红色

我在这方面也遇到了同样的问题,我最终使用了那个库->它更可靠,提供了更多的功能


有了它,您不需要嵌套元素,您可以修改小标签颜色和错误消息…

您可以检查
文本输入布局的代码
现在它可以与
框架布局
一起使用

public TextInputLayout(Context context, AttributeSet attrs, int defStyleAttr) {
    // Can't call through to super(Context, AttributeSet, int) since it doesn't exist on API 10
    super(context, attrs);
    //...
    mInputFrame = new FrameLayout(context);
    mInputFrame.setAddStatesFromChildren(true);
    addView(mInputFrame);

    //....

}

@Override
public void addView(View child, int index, final ViewGroup.LayoutParams params) {
    if (child instanceof EditText) {
        mInputFrame.addView(child, new FrameLayout.LayoutParams(params));
        //...
     } else {
        // Carry on adding the View...
        super.addView(child, index, params);
    }
}
其中
mInputFrame
是一个
FrameLayout

这就是问题的原因(父级是
FrameLayout

java.lang.ClassCastException:android.widget.FrameLayout无法转换为android.support.design.widget.TextInputLayout


你应该使用
TextInputLayout
作为参数,而不是在视图层次结构中导航。

我想知道,像这样简单的事情为什么需要解决办法?@Shaishav我认为这是一种反模式-他们总是说层次结构应该是平面的,而这一个增加了两个额外的层次,一开始EditText是对此负责-只有一个视图。我不认为这是一个好的解决方案。Android支持库团队可以随时更改它,而且您正在视图层次结构中进行循环导航,并测试每个视图是否为实例。只需使用InputExtLayout作为参数。解决方案本身不是最好的,但它考虑到了缺点他们会在下一个版本中删除额外的视图组,这段代码会起作用。我不认为这是问题的解决方案,但似乎我会以自由党结束,设计自由民主党人改变主意太快:)投票赞成!但是您可以在本地添加库,以避免对它们进行任何进一步的修改。或者只指定要用作静态库的库的版本。您可能没有阅读该问题,因为您不理解该问题。当然,您可以按id查找TextInputLayout并在那里设置错误,但关键是您有EditText,需要设置错误。@ViktorYakunin设置错误不需要EditText。你只需要输入布局。您仅使用editText获取父视图。只需使用受保护的void clearError(@NonNull inputExtLayout){….}可能我的英语不好,我没有TextInputLayout的id,只有EditText的引用。如果您仔细阅读代码,您会发现我已经在使用TextInputLayout.OK的方法,但只需向InputTextLayout添加一个id,就可以在此视图上调用方法。读取代码时,只需在此视图上调用setError()。
public TextInputLayout(Context context, AttributeSet attrs, int defStyleAttr) {
    // Can't call through to super(Context, AttributeSet, int) since it doesn't exist on API 10
    super(context, attrs);
    //...
    mInputFrame = new FrameLayout(context);
    mInputFrame.setAddStatesFromChildren(true);
    addView(mInputFrame);

    //....

}

@Override
public void addView(View child, int index, final ViewGroup.LayoutParams params) {
    if (child instanceof EditText) {
        mInputFrame.addView(child, new FrameLayout.LayoutParams(params));
        //...
     } else {
        // Carry on adding the View...
        super.addView(child, index, params);
    }
}