【发布时间】:2020-04-25 09:50:00
【问题描述】:
我想使用非活动类中的 R.style.Widget_MaterialComponents_TextInputLayout_OutlinedBox 样式创建一个带边框的 TextInputLayout。
下面是示例代码:
TextInputLayout textInputLayout = new TextInputLayout(context, null, R.style.Widget_MaterialComponents_TextInputLayout_OutlinedBox);
textInputLayout.setHint("My Hint");
textInputLayout.setBoxBackgroundMode(TextInputLayout.BOX_BACKGROUND_OUTLINE);
textInputLayout.setBoxCornerRadii(5, 5, 5, 5);
TextInputEditText et = new TextInputEditText(textInputLayout.getContext());
textInputLayout.addView(et);
当我运行它时,我得到以下崩溃:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.tsp.bonw, PID: 10121
java.lang.NullPointerException: Attempt to invoke virtual method 'float com.google.android.material.shape.MaterialShapeDrawable.getTopLeftCornerResolvedSize()' on a null object reference
at com.google.android.material.textfield.TextInputLayout.setBoxCornerRadii(TextInputLayout.java:938)
我的猜测是我无法将样式属性:R.style.Widget_MaterialComponents_TextInputLayout_OutlinedBox 从非活动类设置为 TextInputLayout。
如果我删除调用 setBoxCornerRadii,TextInputLayout 会在我的活动中正确显示,但没有框。我需要 TextInputLayout 与框一起显示。
非常感谢任何帮助。 谢谢。
【问题讨论】:
-
为什么不将活动上下文传递给此类?
-
我在活动上下文中也遇到了同样的错误。我不认为是这样。我认为这是一个错误。检查这些链接。 github.com/material-components/material-components-android/…stackoverflow.com/questions/60567668/…
标签: android android-textinputlayout