按下“设置”按钮时,DatePicker和TimePicker崩溃

时间:2015-12-09 15:48:29

标签: java android datepicker timepicker

当我点击按钮时,我正在制作一个有日期和时间选择器的信使。当我按下“设置”按钮显示它们时,应用程序崩溃了。我的代码是:

    private DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() {
    @Override
    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
        yearSet = year;
        monthSet = monthOfYear + 1;
        daySet = dayOfMonth;
        btnDate.setText(yearSet + monthSet + daySet);
        }
    };

    private TimePickerDialog.OnTimeSetListener timePickerListener = new TimePickerDialog.OnTimeSetListener() {
    @Override
    public void onTimeSet(TimePicker view, int hour, int minute) {
        hourSet = hour;
        minuteSet = minute;
        btnTime.setText(hourSet + minuteSet);
        }
    };

logcat的:

    E/AndroidRuntime: FATAL EXCEPTION: main
          android.content.res.Resources$NotFoundException: String resource ID #0x7f4
          at android.content.res.Resources.getText(Resources.java:1068)
          at android.widget.TextView.setText(TextView.java:4546)
          at devncode.kemo.testApp.SendMessageActivity$3.onDateSet(SendMessageActivity.java:95)
          at android.app.DatePickerDialog.tryNotifyDateSet(DatePickerDialog.java:199)
          at android.app.DatePickerDialog.onClick(DatePickerDialog.java:154)
          at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:185)
          at android.os.Handler.dispatchMessage(Handler.java:99)
          at android.os.Looper.loop(Looper.java:176)
          at android.app.ActivityThread.main(ActivityThread.java:5419)
          at java.lang.reflect.Method.invokeNative(Native Method)
          at java.lang.reflect.Method.invoke(Method.java:525)
          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
          at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:2)

您的问题是您将不同项目的文本设置为整数。例如btn.setText(int);它应该是字符串btn.setText(String);

所以要修复它,无论你有什么类似

btnTime.setText(hourSet + minuteSet);

将其更改为

btnTime.setText(hourSet + minuteSet + "");

添加+ ""会将其转换为字符串而不会出错。