简体   繁体   中英

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). I am using datePicker in it and changing the color of selected date to white using following code:

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

dayET.setTextColor(Color.WHITE);

In Jelly beans, I am not getting Edit text view in 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 . 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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