我使用微调器在Calendar类上显示日期。
我可以得到年份和月份,但我无法在spinner
获得一天的价值。
显示
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference
我该如何解决这个问题?
以下是代码:
private void datePick(){
Calendar calendar = Calendar.getInstance();
for (int i = 0; i < 100; i++) {
dataYear.add("" + (calendar.get(Calendar.YEAR) - i));
}
adapterSpYear = new ArrayAdapter<String>(getActivity(), R.layout.spinner, dataYear);
adapterSpYear.setDropDownViewResource(R.layout.support_simple_spinner_dropdown_item);
spYear.setAdapter(adapterSpYear);
spYear.setSelection(34);//for 1982
for (int i = 1; i <= 12; i++) {
dataMonth.add("" + (i < 10 ? "0" + i : i));
}
adapterSpMonth = new ArrayAdapter<String>(getActivity(), R.layout.spinner, dataMonth);
adapterSpMonth.setDropDownViewResource(R.layout.support_simple_spinner_dropdown_item);
spMonth.setAdapter(adapterSpMonth);
adapterSpDay = new ArrayAdapter<String>(getActivity(), R.layout.spinner, dataDay);
adapterSpDay.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spDay.setAdapter(adapterSpDay);
spMonth.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int arg2, long arg3) {
dataDay.clear();
Calendar calendar1 = Calendar.getInstance();
calendar1.set(Calendar.YEAR, Integer.valueOf(spYear.getSelectedItem().toString()));
calendar1.set(Calendar.MONTH, arg2);
int dayOfMonth = calendar1.getActualMaximum(Calendar.DAY_OF_MONTH);
for (int i = 1; i <= dayOfMonth; i++) {
dataDay.add("" + (i < 10 ? "0" + i : i));
}
adapterSpDay.notifyDataSetChanged();
String year=spYear.getSelectedItem().toString();
String month=spMonth.getSelectedItem().toString();
String day=spDay.getSelectedItem().toString();
Log.i("test",".>>>>Year>>>>"+year);
Log.i("test",".>>>>Month>>>>"+month);
Log.i("test",".>>>>Day>>>>"+day);
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
}
答案 0 :(得分:0)
在Spinner Adapter adapterSpDay.notifyDataSetChanged();
中不会刷新适配器数据。您必须在
spMonth.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int arg2, long arg3)
{ // Here create adapter for day again by passing new data and set that adapter to day spinner.
............
}