有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java NoSuchFieldException:NumberPicker Android中的mSelectorWheelPaint

我用这个代码来更改我的号码簿

乌提尔斯。爪哇:

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。爪哇:

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 安卓.view.View.callOnClick(View.java:5718)
                                                                               at tecniva.mx.fillgas.AmountProduct.didClickLyRbtL(AmountProduct.java:288)
                                                                               at java.lang.reflect.Method.invoke(Native Method)
                                                                               at 安卓.view.View$DeclaredOnClickListener.onClick(View.java:4735)
                                                                               at 安卓.view.View.performClick(View.java:5697)
                                                                               at 安卓.view.View$PerformClick.run(View.java:22526)
                                                                               at 安卓.os.Handler.handleCallback(Handler.java:739)
                                                                               at 安卓.os.Handler.dispatchMessage(Handler.java:95)
                                                                               at 安卓.os.Looper.loop(Looper.java:158)
                                                                               at 安卓.app.ActivityThread.main(ActivityThread.java:7224)
                                                                               at java.lang.reflect.Method.invoke(Native Method)
                                                                               at com.安卓.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
                                                                               at com.安卓.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

第行出现错误:

.getField("mSelectorWheelPaint");

上面说找不到mSelectorWheelPaint字段,如何修复

谢谢你


共 (1) 个答案

  1. # 1 楼答案

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

    getDeclaredField代替getField