gpt4 book ai didi

java - NoSuchFieldException:NumberPicker Android 中的 mSelectorWheelPaint

转载 作者:太空宇宙 更新时间:2023-11-04 12:04:56 24 4
gpt4 key购买 nike

我正在使用此代码更改我的 NumberPicker

Utils.java:

public static boolean setNumberPickerTextColor(NumberPicker numberPicker, int color) {

boolean result = false;
final int count = numberPicker.getChildCount();
for(int i = 0; i < count; i++){
View child = numberPicker.getChildAt(i);
if(child instanceof EditText){

try{
Field selectorWheelPaintField = numberPicker.getClass()
.getField("mSelectorWheelPaint");
selectorWheelPaintField.setAccessible(true);
((Paint)selectorWheelPaintField.get(numberPicker)).setColor(color);
((EditText)child).setTextColor(color);
numberPicker.invalidate();
result = true;

}
catch(NoSuchFieldException e){
Log.w("NoSuchFieldException: ", e);
}
catch(IllegalAccessException e){
Log.w("IllegalAccessException: ", e);
}
catch(IllegalArgumentException e){
Log.w("IllegalArgumentException:" , e);
}
}
}
return result;
}

我这样调用它:

AmoutProduct.java:

Utils.setNumberPickerTextColor(np0, ContextCompat.getColor(getApplicationContext(), R.color.colorPrimary));

但我收到此错误:

11-07 13:36:46.573 27277-27277/tecniva.mx.fillgas W/NoSuchFieldException:: java.lang.NoSuchFieldException: mSelectorWheelPaint
at java.lang.Class.getField(Class.java:897)
at tecniva.mx.fillgas.util.Utils.setNumberPickerTextColor(Utils.java:467)
at tecniva.mx.fillgas.AmountProduct.enableLitersOption(AmountProduct.java:273)
at tecniva.mx.fillgas.AmountProduct.access$000(AmountProduct.java:37)
at tecniva.mx.fillgas.AmountProduct$2.onClick(AmountProduct.java:102)
at android.view.View.callOnClick(View.java:5718)
at tecniva.mx.fillgas.AmountProduct.didClickLyRbtL(AmountProduct.java:288)
at java.lang.reflect.Method.invoke(Native Method)
at android.view.View$DeclaredOnClickListener.onClick(View.java:4735)
at android.view.View.performClick(View.java:5697)
at android.view.View$PerformClick.run(View.java:22526)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7224)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

错误发生在行:

.getField("mSelectorWheelPaint");

它说找不到字段 mSelectorWheelPaint,如何解决这个问题?

提前致谢

最佳答案

numberPicker.getClass().getDeclaredField("mSelectorWheelPaint");

使用getDeclaredField而不是getField

关于java - NoSuchFieldException:NumberPicker Android 中的 mSelectorWheelPaint,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40473288/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com