制作闹钟,为什么我的日历为空?
Making an alarm clock, why is my Calendar null?
今年夏天,为了学习应用程序开发,我决定在 Android Studio 中制作一个闹钟,而没有太多 Java 或 OOP 的先验知识。
我正在尝试使用 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();
}
});
}}
很遗憾,我收到了一个 NullPointerException,这意味着我的日历设置不正确,当我使用 AlarmManager 时它为空?
java.lang.NullPointerException: Attempt to invoke virtual method 'long java.util.Calendar.getTimeInMillis()' on a null object reference
at xxxxxx.englishalarmclock.AlarmParameters.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... 内部设置日历,但我不知道如何在外部时使用 int selectedHour/Minute(onTimeSet() 的参数) TimePickerDialog 的东西。当我尝试在 onTimeSet() 中设置 selectedHour/Minute 的 public 变量时,它说这是不允许的。
所以,最后我有 3 个问题:
1.当我在 onTimeSet() 中设置它们时,为什么我的日历没有收到 selectedHour/Minute 值?
2。为什么我 "not allowed" 在 onTimeSet() 中设置一个 public 变量?
3。我将如何在 onTimeSet() 之外使用 selectedHour/Minute 变量?
感谢您的宝贵时间!
--另外,如果您发现我做错了什么或者我可以做得更好,请告诉我!我是来学习的! \(•◡•) /
您重新声明了 mCurrentTime。只需删除第 final Calendar mcurrentTime = Calendar.getInstance();
行的 final Calendar
今年夏天,为了学习应用程序开发,我决定在 Android Studio 中制作一个闹钟,而没有太多 Java 或 OOP 的先验知识。
我正在尝试使用 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();
}
});
}}
很遗憾,我收到了一个 NullPointerException,这意味着我的日历设置不正确,当我使用 AlarmManager 时它为空?
java.lang.NullPointerException: Attempt to invoke virtual method 'long java.util.Calendar.getTimeInMillis()' on a null object reference
at xxxxxx.englishalarmclock.AlarmParameters.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... 内部设置日历,但我不知道如何在外部时使用 int selectedHour/Minute(onTimeSet() 的参数) TimePickerDialog 的东西。当我尝试在 onTimeSet() 中设置 selectedHour/Minute 的 public 变量时,它说这是不允许的。
所以,最后我有 3 个问题:
1.当我在 onTimeSet() 中设置它们时,为什么我的日历没有收到 selectedHour/Minute 值?
2。为什么我 "not allowed" 在 onTimeSet() 中设置一个 public 变量?
3。我将如何在 onTimeSet() 之外使用 selectedHour/Minute 变量?
感谢您的宝贵时间!
--另外,如果您发现我做错了什么或者我可以做得更好,请告诉我!我是来学习的! \(•◡•) /
您重新声明了 mCurrentTime。只需删除第 final Calendar mcurrentTime = Calendar.getInstance();
final Calendar