我建了一个闹钟,我有一个待释放的内容,应该打瞌睡闹钟。当它在Activity中时,相同的代码工作得很好。我改变了一些东西,现在它改变了一个不同的类。我得到一个nullpointerExecption。 (承包商从活动中获取背景信息。
延后功能:
public void settingNewIntentForSnooze() {
PendingIntent alarmPendingIntent=null;
Calendar calendar = Calendar.getInstance();
// new alarm after the snooze.
calendar.set(Calendar.MINUTE, calendar.get(Calendar.MINUTE) + snoozeTime);
Intent snoozeIntent = new Intent(context, Alarm_Reciver.class);
snoozeIntent.putExtra("click_status", true);
//add that to try to solve the problem...
snoozeIntent.setAction("Snooze");
alarmPendingIntent = PendingIntent.getBroadcast(context, 0, snoozeIntent, alarmPendingIntent.FLAG_ONE_SHOT);
//tells to phone to set the alarm
alarm_manager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), alarmPendingIntent);
}
AlarmPage活动(创建变量的代码):
snooze= new Snooze(this,alarm_manager);
AlarmReciver:
public void onReceive(Context context, Intent intent) {
Log.e("in alarm reciver","in alarm reciver");
Intent service_intent= new Intent(context, RingtonePlayingService.class);//intent for the service ringtone playing
boolean button_status = intent.getExtras().getBoolean("click_status");
service_intent.putExtra("click_status", button_status);//passing button status
context.startService(service_intent);//start the ringtone service
}
错误:
09-17 18:25:25.564 1563-1563/com.example.itay.newfrindlyalarm E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.itay.newfrindlyalarm, PID: 1563
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.AlarmManager.set(int, long, android.app.PendingIntent)' on a null object reference
at com.example.itay.newfrindlyalarm.Snooze.settingNewIntentForSnooze(Snooze.java:60)
at com.example.itay.newfrindlyalarm.Snooze.operate(Snooze.java:44)
at com.example.itay.newfrindlyalarm.AlarmPage.operateSnooze(AlarmPage.java:88)
at com.example.itay.newfrindlyalarm.AlarmPage$3.onClick(AlarmPage.java:68)
at android.view.View.performClick(View.java:6256)
at android.view.View$PerformClick.run(View.java:24697)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
enter code here
答案 0 :(得分:0)
根据错误消息判断alarm_manager
null
是否可能是AlarmManager
?如果正确初始化TextView tv;
Calendar mCurrentDate;
int day, month, year;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tuto4);
tv = (TextView) findViewById(R.id.datePicker);
mCurrentDate = Calendar.getInstance();
day = mCurrentDate.get(Calendar.DAY_OF_MONTH);
month = mCurrentDate.get(Calendar.MONTH);
year = mCurrentDate.get(Calendar.YEAR);
month = month + 1;
tv.setText(day+"/"+month+"/"+year);
tv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick (View v)
{
final Calendar calendar = Calendar.getInstance();
int yy = calendar.get(Calendar.YEAR);
int mm = calendar.get(Calendar.MONTH);
int dd = calendar.get(Calendar.DAY_OF_MONTH);
DatePickerDialog datePicker = new DatePickerDialog(Tuto4Activity.this, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
String date = "Date"+String.valueOf(year) +"-"+String.valueOf(monthOfYear)
+"-"+String.valueOf(dayOfMonth);
tv.setText(date);
tv.setText(date);
}
}, yy, mm, dd);
datePicker.show();
}
});
}
public void onRadioButtonClicked(View view) {
boolean checked = ( (RadioButton) view). isChecked();
switch (view.getId()) {
case R.id.radioButton1:
if (checked)
setContentView(R.layout.activity_tuto2);
break;
case R.id.radioButton2:
if (checked)
setContentView(R.layout.activity_tuto3);
break;
case R.id.radioButton3:
if (checked)
setContentView(R.layout.activity_tuto4);
break;
}
}
}
,错误是否会消失?