简体   繁体   English

Android 4.1和4.2中的DatePicker问题

[英]DatePicker issue in android 4.1 and 4.2

I am developing an android app for all the versions (version 2.2 to 4.2). 我正在为所有版本(版本2.2至4.2)开发一个android应用。 I am using datePicker in it and changing the color of selected date to white using following code: 我在其中使用datePicker,并使用以下代码将所选日期的颜色更改为白色:

if (currentapiVersion >= 14) {

    ViewGroup dayChildpicker = (ViewGroup) datePicker
            .findViewById (Resources.getSystem().getIdentifier("day", "id", "android"));
    ViewGroup monthChildpicker = (ViewGroup) datePicker
            .findViewById(Resources.getSystem().getIdentifier("month", "id", "android"));
    ViewGroup yearChildpicker = (ViewGroup) datePicker
            .findViewById(Resources.getSystem().getIdentifier("year", "id", "android"));

    EditText dayET = null;
    EditText mornthET = null;
    EditText yearET = null;

    dayET = (EditText) dayChildpicker.getChildAt(1);
    mornthET = (EditText) monthChildpicker.getChildAt(1);
    yearET = (EditText) yearChildpicker.getChildAt(1);

    dayET.setTextColor(Color.WHITE); // null pointer error on this line
    mornthET.setTextColor(Color.WHITE);
    yearET.setTextColor(Color.WHITE);
}

But this is throwing null pointer exception in android 4.1 and 4.2(Jelly beans) at the line 但这会在android 4.1和4.2(果冻豆)中抛出空指针异常

dayET.setTextColor(Color.WHITE);

In Jelly beans, I am not getting Edit text view in datepicker. 在果冻豆中,我没有在datepicker中获得“编辑文本”视图。

What is the method to get childs of date picker in jellybeans and how to change text color of selected date in date picker. 在豆形软糖中获取日期选择器子项的方法是什么,以及如何在日期选择器中更改所选日期的文本颜色。

Please guide me. 请指导我。

I'm not sure if getChildAt(1) actually gets the control since there may be no guarantee what order a control is in the ViewGroup . 我不确定getChildAt(1)真正获得了控件,因为可能无法保证ViewGroup控件的顺序。 However you should be able to reference these differently through an identifier 但是,您应该能够通过标识符以不同的方式引用这些内容

Change these 改变这些

dayET = (EditText) dayChildpicker.getChildAt(1);
mornthET = (EditText) monthChildpicker.getChildAt(1);
yearET = (EditText) yearChildpicker.getChildAt(1);

To these 对于这些

dayET = (EditText) dayChildpicker.findViewById(Resources.getSystem().getIdentifier("numberpicker_input", "id",  "android"));
mornthET = (EditText) monthChildpicker.findViewById(Resources.getSystem().getIdentifier("numberpicker_input", "id",  "android"));
yearET = (EditText) yearChildpicker.findViewById(Resources.getSystem().getIdentifier("numberpicker_input", "id",  "android"));

This should allow you to reference these properly without a null pointer error. 这样可以使您正确引用这些内容,而不会出现空指针错误。

Let me know if you have other questions or it doesn't work. 让我知道您是否还有其他问题或不起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM