我有一个AppCompatSpinner
,我试图在TextView
处设置文字。
这是我的代码:
eSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
final TextView eSpinnerTV = (TextView) adapterView.getSelectedView();
switch (i) {
case 0:
final Calendar c = Calendar.getInstance();
mHourEnd = c.get(Calendar.HOUR_OF_DAY);
mMinuteEnd = c.get(Calendar.MINUTE);
TimePickerDialog timePickerDialog = new TimePickerDialog(activityName.this,
new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay,
int minute) {
Calendar date = Calendar.getInstance();
date.set(Calendar.HOUR_OF_DAY, hourOfDay);
date.set(Calendar.MINUTE, minute);
date.set(Calendar.AM_PM, date.get(Calendar.AM_PM));
sEnd(hourOfDay, minute);
uTimeInSF = new SimpleDateFormat("HH:mm:ss").format(date.getTime());
eString = new SimpleDateFormat("hh:mm a").format(date.getTime());
// error on the line below
eSpinnerTV.setText(uTimeAMPM);
}
}, mHourEnd, mMinuteEnd, false);
timePickerDialog.show();
}
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
此处的sEnd.java
文件代码:
public void sEnd(int hour, int min) {
if (hour == 0) {
hour += 12;
format = "AM";
} else if (hour == 12) {
format = "PM";
} else if (hour > 12) {
hour -= 12;
format = "PM";
} else {
format = "AM";
}
uTimeAMPM = hour + ":" + min + " " + format;
}
这里是xml
代码:
<android.support.v7.widget.AppCompatSpinner
android:id="@+id/pTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:entries="@array/pTimeList"
style="@style/Base.Widget.AppCompat.Spinner">
</android.support.v7.widget.AppCompatSpinner>
此处pTimeList
:
<string-array name="pTimeList">
<item>Pick a time..</item>
</string-array>
我在从OK
选择时间后点击TimePickerDialog
按钮,应用程序崩溃时会出现此错误:java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
在上面代码中指定的行
即使此文本视图已在代码中明确引用为final TextView eSpinnerTV = (TextView) adapterView.getSelectedView();
,导致此错误的原因是什么?
请告诉我。
答案 0 :(得分:3)
这样做
eSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
**final TextView eSpinnerTV = (TextView) adapterView.getChildAt(position)**
switch (i) {
case 0:
final Calendar c = Calendar.getInstance();
mHourEnd = c.get(Calendar.HOUR_OF_DAY);
mMinuteEnd = c.get(Calendar.MINUTE);
TimePickerDialog timePickerDialog = new TimePickerDialog(activityName.this,
new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay,
int minute) {
Calendar date = Calendar.getInstance();
date.set(Calendar.HOUR_OF_DAY, hourOfDay);
date.set(Calendar.MINUTE, minute);
date.set(Calendar.AM_PM, date.get(Calendar.AM_PM));
sEnd(hourOfDay, minute);
uTimeInSF = new SimpleDateFormat("HH:mm:ss").format(date.getTime());
eString = new SimpleDateFormat("hh:mm a").format(date.getTime());
// error on the line below
eSpinnerTV.setText(uTimeAMPM);
}
}, mHourEnd, mMinuteEnd, false);
timePickerDialog.show();
}
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
答案 1 :(得分:2)
不使用adapterView
,而是使用view
的{{1}}对象第二个参数从Spinner获取选定的行视图:
onItemSelected