1

I'm using this code for changing my 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;
    }

And I call it like this:

AmoutProduct.java:

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

but I get this error:

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)

Error occurs at line:

.getField("mSelectorWheelPaint");

it says that doesn't find the field mSelectorWheelPaint, how to fix that?

thanks in advace

Jesús Ayala
  • 2,743
  • 3
  • 32
  • 48

1 Answers1

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

use getDeclaredField instead of getField

Ankur Jain
  • 507
  • 3
  • 9
  • 3
    This will not work on Android 10+ anymore. It's tagged as "No public alternative. Developers should never access this field directly." Here: https://developer.android.com/about/versions/10/non-sdk-q – Alejandro H. Cruz Oct 20 '20 at 09:59