TextInputLayout setError 方法在 24.2.0 中抛出 ClassCastException
TextInputLayout setError method throws ClassCastException in 24.2.0
我将支持库版本更新到 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>
所以,这工作得很好,但现在它抛出 ClassCastException:
java.lang.ClassCastException: android.widget.FrameLayout cannot be cast to android.support.design.widget.TextInputLayout
我想知道是否有关于此的一些新指南?
问题调查显示由于某种原因有额外的布局,所以现在我们有 TextInputLayout -> FrameLayout -> TextInputEditText ,这很遗憾:(所以作为临时解决方法,我创建了以下方法:
@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。
如果你幸运的话,你会注意到这个粉红色的错误颜色变化,克服它的简单方法是覆盖样式,在 styles.xml:
<style name="TextAppearance.Design.Error" parent="TextAppearance.AppCompat.Caption" tools:override="true">
<item name="android:textColor">@color/red</item>
</style>
我遇到了同样的问题,最后我使用了那个库 -> MaterialEditTest,它更可靠并且提供了更多的功能。
有了那个,你不需要嵌套元素,你可以修改小标签颜色和错误信息...
您可以查看 TextInputLayout
v24.2.x.
的代码
现在它适用于 FrameLayout
.
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 cannot be cast to android.support.design.widget.TextInputLayout
您应该使用 TextInputLayout
作为参数,而不是在视图层次结构中导航。
我将支持库版本更新到 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>
所以,这工作得很好,但现在它抛出 ClassCastException:
java.lang.ClassCastException: android.widget.FrameLayout cannot be cast to android.support.design.widget.TextInputLayout
我想知道是否有关于此的一些新指南?
问题调查显示由于某种原因有额外的布局,所以现在我们有 TextInputLayout -> FrameLayout -> TextInputEditText ,这很遗憾:(所以作为临时解决方法,我创建了以下方法:
@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。
如果你幸运的话,你会注意到这个粉红色的错误颜色变化,克服它的简单方法是覆盖样式,在 styles.xml:
<style name="TextAppearance.Design.Error" parent="TextAppearance.AppCompat.Caption" tools:override="true">
<item name="android:textColor">@color/red</item>
</style>
我遇到了同样的问题,最后我使用了那个库 -> MaterialEditTest,它更可靠并且提供了更多的功能。
有了那个,你不需要嵌套元素,你可以修改小标签颜色和错误信息...
您可以查看 TextInputLayout
v24.2.x.
的代码
现在它适用于 FrameLayout
.
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 cannot be cast to android.support.design.widget.TextInputLayout
您应该使用 TextInputLayout
作为参数,而不是在视图层次结构中导航。