Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/202.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 制作闹钟,为什么我的日历是空的?_Android_Android Studio_Alarm_Android Calendar_Android Timepicker - Fatal编程技术网

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

Android 制作闹钟,为什么我的日历是空的?,android,android-studio,alarm,android-calendar,android-timepicker,Android,Android Studio,Alarm,Android Calendar,Android Timepicker,今年夏天,为了学习应用程序开发,我决定在没有Java或OOP知识的情况下在Android Studio中制作闹钟 我试图使用TimePickerDiaglog获取用户对报警的输入,然后将该时间存储在日历实用程序中,这样我就可以使用报警管理器设置一个挂起的意图 public class AlarmParameters extends AppCompatActivity { EditText time; Button btn_set_alarm; TimePickerDial

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

我试图使用TimePickerDiaglog获取用户对报警的输入,然后将该时间存储在日历实用程序中,这样我就可以使用报警管理器设置一个挂起的意图

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();
            }
        });
    }}
所以我想问题就归结到我在时间选择器对话框中说这些的地方

//store time picked in the calendar
mcurrentTime.set(Calendar.HOUR_OF_DAY, selectedHour);
mcurrentTime.set(Calendar.MINUTE, selectedMinute);
我试着这样做,设置日历,在btn_set_alarm.setOnclickListener中。。。但是我不知道在TimePickerDialog之外如何使用int-selectedHour/Minute(onTimeSet()的参数)。当我尝试在onTimeSet()中设置一个公共变量selectedHour/Minute时,它说这是不允许的

最后,我有3个问题:

1。为什么在onTimeSet()中设置选定的小时/分钟值时,我的日历没有收到这些值?

2。为什么我“不允许”在onTimeSet()中设置公共变量?

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

谢谢你抽出时间


--还有,如果你看到我做错了什么,或者我可以做得更好,请告诉我!我是来学习的(•◡•) /

您重新声明了mCurrentTime。只需删除第
行的
最终日历mCurrentTime=Calendar.getInstance();
您重新声明了mCurrentTime。只需删除第
行的
最终日历mCurrentTime=Calendar.getInstance()

谢谢您的回复!很遗憾,现在我遇到了另一个错误:
java.lang.NullPointerException:尝试调用虚拟方法'int java.util.Calendar.get(int)'在空对象引用上
由于某种原因,它认为日历仍然为空,即使它刚刚得到实例?请在帖子中编辑您的代码,并告诉我哪一行与此错误相关。别担心!我在编辑代码时出错了。它现在可以工作了!非常感谢!感谢您的回复!很遗憾,现在我又收到了另一行错误错误:
java.lang.NullPointerException:尝试调用虚拟方法'int java.util.Calendar.get(int)'在空对象引用上
出于某种原因,它认为日历仍然为空,即使它刚刚得到实例?请在帖子中编辑您的代码,并告诉我哪一行与此错误相关。别担心!我在编辑代码时出错了。现在可以了!非常感谢!
//store time picked in the calendar
mcurrentTime.set(Calendar.HOUR_OF_DAY, selectedHour);
mcurrentTime.set(Calendar.MINUTE, selectedMinute);