制作一个闹钟,为什么我的日历是空的?

时间:2017-07-22 00:48:22

标签: android android-studio alarm android-calendar android-timepicker

今年夏天,为了学习应用程序开发,我决定在没有Java或OOP的先验知识的情况下在Android Studio中制作闹钟。

我正在尝试使用TimePickerDiaglog获取用户的警报输入,然后将该时间存储在Calendar实用程序中,因此我可以使用Alarm Manager设置待处理的意图。

public class AlarmParameters extends AppCompatActivity {
    EditText time;
    Button btn_set_alarm;
    TimePickerDialog mTimePicker;
    Calendar mcurrentTime;
    AlarmManager alarmManager;
    private PendingIntent pending_intent;
    private AlarmReceiver alarm;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_alarm_parameters);

        //match ids
        btn_set_alarm = (Button) findViewById(R.id.parameters_btn_set_alarm);
        time = (EditText) findViewById(R.id.time_box_text);

        //remove pesky keyboard
        time.setInputType(InputType.TYPE_NULL);

        // perform click event listener on edit text
        time.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                //retrieve current time
                final Calendar mcurrentTime = Calendar.getInstance();
                int hour = mcurrentTime.get(Calendar.HOUR_OF_DAY);
                int minute = mcurrentTime.get(Calendar.MINUTE);

                mTimePicker = new TimePickerDialog(AlarmParameters.this, new TimePickerDialog.OnTimeSetListener() {
                    @Override
                    public void onTimeSet(TimePicker timePicker, int selectedHour, int selectedMinute) {
                        //adds the 0, like : 10:6 AM --> 10:06 AM
                        if(selectedMinute<10){
                            time.setText(selectedHour + ":0" + selectedMinute);
                        }
                        else {
                            time.setText(selectedHour + ":" + selectedMinute);
                        }

                        //store time picked in the calendar
                        mcurrentTime.set(Calendar.HOUR_OF_DAY, selectedHour);
                        mcurrentTime.set(Calendar.MINUTE, selectedMinute);

                        // no seconds in the calendar
                        mcurrentTime.add(Calendar.SECOND, 0);
                    }
                }, hour, minute, true);//Yes 24 hour time

                //more settings for dialog window
                mTimePicker.setTitle("Select Time");
                mTimePicker.show();
            }
        });

        //actions when you hit the set alarm text
        btn_set_alarm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //get the time of the alarm set from text box
                String alarm_set_string = String.valueOf(time.getText());

                //add that alarm to list of alarms on first page
                MainActivity.ALARMARRAY.add(alarm_set_string);

                //create intent to send to alarmReceiver
                final Intent myIntent = new Intent(AlarmParameters.this, AlarmReceiver.class);

                //set up alarm manager
                alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);

                //make the myIntent a pending intent?
                pending_intent = PendingIntent.getBroadcast(AlarmParameters.this, 0, myIntent, PendingIntent.FLAG_UPDATE_CURRENT);

                //tell alarmManager to send the pending intent at the time selected by the timepicker (in calendar now)
                alarmManager.set(AlarmManager.RTC_WAKEUP, mcurrentTime.getTimeInMillis(), pending_intent);//error here

                Log.e("BUTTON", "alarm set");
                finish();
            }
        });
    }}

可悲的是,我收到一个NullPointerException,这意味着我的Calendar没有正确设置,当我使用AlarmManager时它是null?

java.lang.NullPointerException: Attempt to invoke virtual method 'long java.util.Calendar.getTimeInMillis()' on a null object reference
                                                                                at xxxxxx.englishalarmclock.AlarmParameters$2.onClick(AlarmParameters.java:95)

所以我认为这个问题归结为我在TimePickerDialog中所说的内容。

//store time picked in the calendar
mcurrentTime.set(Calendar.HOUR_OF_DAY, selectedHour);
mcurrentTime.set(Calendar.MINUTE, selectedMinute);

我尝试这样做,在btn_set_alarm.setOnclickListener中设置Calendar,但是当在TimePickerDialog-thing之外时,我无法弄清楚如何使用int selectedHour / Minute(onTimeSet()的参数)。当我尝试在onTimeSet()中设置selectedHour / Minute的公共变量时,它表示不允许这样做。

所以,最终我有3个问题:

1。当我在onTimeSet()中设置时,为什么我的日历没有收到selectedHour / Minute值?

2。为什么我&#34;不被允许&#34;在onTimeSet()中设置公共变量?

第3。我如何在onTimeSet()之外使用selectedHour / Minute变量?

感谢您的时间!

- 如果你看到其他任何我做错了或者我可以做得更好,请告诉我!我在这里学习! \(•◡•)/

1 个答案:

答案 0 :(得分:0)

您重新声明了mCurrentTime。只需删除第final Calendar行<{1}}

即可