我有按钮和textView。当我点击按钮时,timePicker将显示出来。但是,当我完成设置一小时和一分钟并将在TextView中显示它时,它很遗憾地停止
我的代码:
protected Dialog onCreateDialog(int id) {
switch (id) {
case TIME_DIALOG_ID:
return new TimePickerDialog(this, timePickerListener, hour, minute, true);
}
return null;
}
private TimePickerDialog.OnTimeSetListener timePickerListener =
new TimePickerDialog.OnTimeSetListener() {
public void onTimeSet(TimePicker view, int selectedHour,int selectedMinute) {
hour = selectedHour;
minute = selectedMinute;
//error here================= What's Wrong?
tvDisplayTime.setText(new StringBuilder().append(pad(hour)).append(":")
.append(pad(minute)));
}};
// ===============================
private static String pad(int c) {
if (c >= 10)
return String.valueOf(c);
else
return "0" + String.valueOf(c);
}
}
为什么当我尝试从TimePicker设置小时和分钟并在TextView中显示总是错误?
请帮忙......
记录:
07-27 18:14:24.661: E/SpannableStringBuilder(7324): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
07-27 18:14:24.661: E/SpannableStringBuilder(7324): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
07-27 18:14:30.901: D/AndroidRuntime(7324): Shutting down VM
07-27 18:14:30.901: W/dalvikvm(7324): threadid=1: thread exiting with uncaught exception (group=0x42034438)
07-27 18:14:30.911: E/AndroidRuntime(7324): FATAL EXCEPTION: main
07-27 18:14:30.911: E/AndroidRuntime(7324): java.lang.NullPointerException
07-27 18:14:30.911: E/AndroidRuntime(7324): at com.example.b_timepicker.MainActivity$1.onTimeSet(MainActivity.java:98)
07-27 18:14:30.911: E/AndroidRuntime(7324): at android.app.TimePickerDialog.tryNotifyTimeSet(TimePickerDialog.java:201)
07-27 18:14:30.911: E/AndroidRuntime(7324): at android.app.TimePickerDialog.onClick(TimePickerDialog.java:165)
07-27 18:14:30.911: E/AndroidRuntime(7324): at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:167)
07-27 18:14:30.911: E/AndroidRuntime(7324): at android.os.Handler.dispatchMessage(Handler.java:99)
07-27 18:14:30.911: E/AndroidRuntime(7324): at android.os.Looper.loop(Looper.java:137)
07-27 18:14:30.911: E/AndroidRuntime(7324): at android.app.ActivityThread.main(ActivityThread.java:4963)
07-27 18:14:30.911: E/AndroidRuntime(7324): at java.lang.reflect.Method.invokeNative(Native Method)
07-27 18:14:30.911: E/AndroidRuntime(7324): at java.lang.reflect.Method.invoke(Method.java:511)
07-27 18:14:30.911: E/AndroidRuntime(7324): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
07-27 18:14:30.911: E/AndroidRuntime(7324): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
07-27 18:14:30.911: E/AndroidRuntime(7324): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
请确保您正确声明tvDisplayTime
TextView
,因此当您致电setText()
时,它不会为空。还要确保在正确的布局上定义了tvDisplayTime
。