1

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

实用程序.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,如何解决?

提前感谢

4

1 回答 1

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

使用getDeclaredField而不是getField

于 2018-03-15T01:45:02.913 回答