问题描述
我将支持库版本更新为24.2.0,现在我的注册屏幕消失了.问题出在TextInputLayout中,我有两种方法:
I updated support lib version to 24.2.0 and my registration screen is dead now. The problem is in the TextInputLayout, I have two methods:
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时出现错误,在布局中我具有以下代码:
I'm getting an error when I'm trying to cast the parent of EditText to TextInputLayout, in the layout I have this kind of code for that:
<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>
所以,这工作得很好,但是现在它抛出ClassCastException:
So, this worked perfectly fine, but now it throws ClassCastException:
java.lang.ClassCastException: android.widget.FrameLayout cannot be cast to android.support.design.widget.TextInputLayout
我想知道是否有一些新的指南吗?
I'm wondering if there some new guides on that?
推荐答案
问题调查表明,由于某种原因,存在额外的布局,因此,我们现在有了TextInputLayout-> FrameLayout-> TextInputEditText,这很可悲:(如temp解决方法,我创建了以下方法:
The problem investigation showed that there is extra layout for some reason, so now we have TextInputLayout -> FrameLayout -> TextInputEditText and that is sad :( so as temp workaround I've created following method:
@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;
}
这将在视图层次结构中找到您的TextInputLayout.
This one will find your TextInputLayout in view hierarchy.
如果幸运的话,您会注意到这种粉红色错误的颜色变化,克服它的简单方法是在styles.xml中覆盖样式:
If you the lucky one you will notice this pink error color change, the easy way to overcome it is override style, in styles.xml:
<style name="TextAppearance.Design.Error" parent="TextAppearance.AppCompat.Caption" tools:override="true">
<item name="android:textColor">@color/red</item>
</style>
这篇关于TextInputLayout setError方法在24.2.0中引发ClassCastException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!