簡體   English   中英

Android 4.1和4.2中的DatePicker問題

[英]DatePicker issue in android 4.1 and 4.2

我正在為所有版本(版本2.2至4.2)開發一個android應用。 我在其中使用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);
}

但這會在android 4.1和4.2(果凍豆)中拋出空指針異常

dayET.setTextColor(Color.WHITE);

在果凍豆中,我沒有在datepicker中獲得“編輯文本”視圖。

在豆形軟糖中獲取日期選擇器子項的方法是什么,以及如何在日期選擇器中更改所選日期的文本顏色。

請指導我。

我不確定getChildAt(1)真正獲得了控件,因為可能無法保證ViewGroup控件的順序。 但是,您應該能夠通過標識符以不同的方式引用這些內容

改變這些

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

對於這些

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"));

這樣可以使您正確引用這些內容,而不會出現空指針錯誤。

讓我知道您是否還有其他問題或不起作用。

暫無
暫無

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

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