使用AlarmManager的闹钟应用程序崩溃

时间:2014-03-12 17:44:38

标签: android broadcastreceiver runtime-error alarmmanager

没有错误,但当我尝试在手机或模拟器上运行时,它会意外关闭。

这是我的主要活动,它调用广播接收器来关闭闹钟。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
     DatePicker datePicker = (DatePicker)findViewById(R.id.datePicker);
        TimePicker timePicker = (TimePicker)findViewById(R.id.timePicker);
        Button btnset = (Button)findViewById(R.id.button1);
        Button btncancel = (Button)findViewById(R.id.button2);

        btnset.setOnClickListener(this); 
        btncancel.setOnClickListener(this);


}
        public void onClick (View v) {

        switch( v.getId()) {

        case R.id.button1:

            Calendar calendar= Calendar.getInstance();
            calendar.set(Calendar.MONTH, datePicker.getMonth());
            calendar.set(Calendar.DAY_OF_MONTH, datePicker.getDayOfMonth());
            calendar.set(Calendar.YEAR, datePicker.getYear());
            calendar.set(Calendar.HOUR_OF_DAY, timePicker.getCurrentHour());
            calendar.set(Calendar.MINUTE, timePicker.getCurrentMinute());




        Intent AlarmIntent = new Intent(MainActivity.this, AlarmReceiver.class);
        AlarmManager AlmMgr = (AlarmManager)getSystemService(ALARM_SERVICE);
        PendingIntent Sender = PendingIntent.getBroadcast(MainActivity.this, 0, AlarmIntent, 0);    
        AlmMgr.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), Sender);
        }
}

这是广播接收器类:

public class AlarmReceiver extends BroadcastReceiver {


    @Override
    public void onReceive(Context context, Intent intent) 
    {   //Build pending intent from calling information to display Notification
        PendingIntent Sender = PendingIntent.getBroadcast(context, 0, intent, 0);
        NotificationManager manager = (NotificationManager)context.getSystemService(android.content.Context.NOTIFICATION_SERVICE);
        Notification noti = new Notification(android.R.drawable.stat_notify_more, "Wake up alarm", System.currentTimeMillis());
        noti.setLatestEventInfo(context, "My Alarm", "WAKE UP...!!!", Sender);
        noti.flags = Notification.FLAG_AUTO_CANCEL;
        manager.notify(R.string.app_name, noti); 

        /* intent to call the activity which shows on ringing
        Intent myIntent = new Intent(context, Alarmring.class);
        myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(myIntent); */

        //display that alarm is ringing
        Toast.makeText(context, "Alarm Ringing...!!!", Toast.LENGTH_LONG).show();
    }}

我也在添加日志错误

03-12 13:11:33.160: E/AndroidRuntime(10625): FATAL EXCEPTION: main 
03-12 13:11:33.160: E/AndroidRuntime(10625): java.lang.NullPointerException 
03-12 13:11:33.160: E/AndroidRuntime(10625): at com.example.myexample.MainActivity.onClick(MainActivity.java:44) 
03-12 13:11:33.160: E/AndroidRuntime(10625): at android.view.View.performClick(View.java:2532) 
03-12 13:11:33.160: E/AndroidRuntime(10625): at android.view.View$PerformClick.run(View.java:9277) 
03-12 13:11:33.160: E/AndroidRuntime(10625): at android.os.Handler.handleCallback(Handler.java:587)

0 个答案:

没有答案