首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NoSuchFieldException: NumberPicker安卓中的mSelectorWheelPaint

NoSuchFieldException: NumberPicker安卓中的mSelectorWheelPaint
EN

Stack Overflow用户
提问于 2016-11-08 03:43:03
回答 1查看 624关注 0票数 1

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

Utils.java:

代码语言:javascript
复制
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:

代码语言:javascript
复制
Utils.setNumberPickerTextColor(np0, ContextCompat.getColor(getApplicationContext(), R.color.colorPrimary));

但是我得到了这个错误:

代码语言:javascript
复制
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)

第1行出现错误:

代码语言:javascript
复制
.getField("mSelectorWheelPaint");

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

提前感谢

EN

回答 1

Stack Overflow用户

发布于 2018-03-15 09:45:03

代码语言:javascript
复制
numberPicker.getClass().getDeclaredField("mSelectorWheelPaint");

使用getDeclaredField而不是getField

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40473288

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档