为什么在Android中为NumberPicker设置textcolor时会发生NoSuchFieldException?
当我为NumberPicker设置文本颜色时为什么在Android中为NumberPicker设置textcolor时会发生NoSuchFieldException?,android,Android,当我为NumberPicker设置文本颜色时 fun updateTextAttributes() { val typeface = Typeface.create(Typeface.DEFAULT, textStyle) try { val wheelPaint = wheelField.get(this) as Paint wheelPaint.textSize = textSize.toFloat()
fun updateTextAttributes() {
val typeface = Typeface.create(Typeface.DEFAULT, textStyle)
try {
val wheelPaint = wheelField.get(this) as Paint
wheelPaint.textSize = textSize.toFloat()
wheelPaint.color = ContextCompat.getColor(context, R.color.member_center_text)
wheelPaint.typeface = typeface
for (i in 0 until childCount) {
val child = getChildAt(i)
if (child is EditText) {
child.setLinkTextColor(ContextCompat.getColor(context, R.color.member_center_text))
child.setHintTextColor(ContextCompat.getColor(context, R.color.member_center_text))
child.setTextColor(ContextCompat.getColorStateList(context, R.color.member_center_text))
child.setTextSize(TypedValue.COMPLEX_UNIT_SP, pixelsToSp(context, textSize.toFloat()))
child.inputType = InputType.TYPE_CLASS_NUMBER or InputType.TYPE_NUMBER_VARIATION_NORMAL
child.typeface = typeface
invalidate()
break
}
}
} catch (e: NoSuchFieldException) {
Timber.d("TypeNumberPicker NoSuchFieldException:$e")
// nothing to do, ignoring
} catch (e: IllegalAccessException) {
Timber.d("TypeNumberPicker IllegalAccessException:$e")
// nothing to do, ignoring
} catch (e: IllegalArgumentException) {
Timber.d("TypeNumberPicker IllegalArgumentException:$e")
// nothing to do, ignoring
}
}
它显示了
NoSuchFieldException:java.lang.NoSuchFieldException: No field mSelectorWheelPaint in class Landroid/widget/NumberPicker; (declaration of 'android.widget.NumberPicker' appears in /system/framework/framework.jar!classes3.dex)
我错过什么了吗?提前谢谢。我想你应该这样做
EditText child = numberPicker.findViewById(android.R.id.numberpicker_input); // Here numberPicker is object which you are using into your code. Replace it with your numberPicker
if (child != null) { // Rename child as meaningfull name
child.setLinkTextColor(ContextCompat.getColor(context, R.color.member_center_text))
child.setHintTextColor(ContextCompat.getColor(context, R.color.member_center_text))
child.setTextColor(ContextCompat.getColorStateList(context, R.color.member_center_text))
child.setTextSize(TypedValue.COMPLEX_UNIT_SP, pixelsToSp(context, textSize.toFloat()))
child.inputType = InputType.TYPE_CLASS_NUMBER or InputType.TYPE_NUMBER_VARIATION_NORMAL
child.typeface = typeface
invalidate()
}
您的代码中的mSelectorWheelPaint是什么?