TimePicker崩溃应用

时间:2015-05-20 07:38:36

标签: android

从TimePicker对话框设置闹钟,但是,当调用intent时,应用程序崩溃。

以下是对话代码:

public void clicker (View view)
{
    Calendar calendar = Calendar.getInstance();

    TimePickerDialog mTimePicker;
    mTimePicker = new TimePickerDialog(AddAlarm.this,
        new TimePickerDialog.OnTimeSetListener() {
            @Override
            public void onTimeSet(TimePicker timePicker,int selectedHour, int selectedMinute) {
                Calendar calendar = Calendar.getInstance();
                calendar.set(Calendar.HOUR_OF_DAY, timePicker.getCurrentHour());
                calendar.set(Calendar.MINUTE, timePicker.getCurrentMinute());

                Intent myIntent = new Intent(AddAlarm.this, AlarmReceiver.class);
                pendingIntent = PendingIntent.getBroadcast(AddAlarm.this, 0, myIntent, 0);

                alarmManager.set(AlarmManager.RTC_WAKEUP,
                    calendar.getTimeInMillis(), 
                    pendingIntent);
            }
        }, hour, minute, false);// Yes 24 hour time

    mTimePicker.setTitle("Select Time");
    mTimePicker.show();
}

这是logcat:

05-20 13:04:08.627  10462-10462/zyia.alarm.zyia.zyia E/AndroidRuntime﹕ FATAL EXCEPTION: main java.lang.NullPointerException
        at zyia.alarm.zyia.zyia.AddAlarm$2.onTimeSet(AddAlarm.java:72)
        at android.app.TimePickerDialog.tryNotifyTimeSet(TimePickerDialog.java:192)
        at android.app.TimePickerDialog.onClick(TimePickerDialog.java:156)
        at com.android.internal.app.AlertController$ButtonHandler.handleMessage (AlertController.java:167)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:4867)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)
        at dalvik.system.NativeStart.main(Native Method)

当我按下ImageButton以启动TimePicker对话框时会发生这种情况。我猜错误在于通过意图传递时间。任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:1)

你已经完成了

 calendar.set(Calendar.HOUR_OF_DAY, timePicker.getCurrentHour());
 calendar.set(Calendar.MINUTE, timePicker.getCurrentMinute());

您应该使用selectedHour和selectedMinute

calendar.set(Calendar.HOUR_OF_DAY, selectedHour);
calendar.set(Calendar.MINUTE, selectedMinute);

这应该可行,并确保使用imageView完成所有操作 一切顺利

答案 1 :(得分:0)

试试这个

to_be_translated