簡體   English   中英

NoSuchFieldException:NumberPicker Android中的mSelectorWheelPaint

[英]NoSuchFieldException: mSelectorWheelPaint in NumberPicker Android

我正在使用此代碼更改我的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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM