简体   繁体   中英

alarm manager date issue

I'm trying to set an alarm manager class up so on a specified date (set by a date picker) a toast style message will be displayed to the user on the specified date basically telling them their appointment is that day.

So far I have a toggle button that calls a separate class to set the alarm manager with the date, month and year variables sent to the constructor.

At the moment I'm getting the following errors:

Do I need to set an activity intent to display the message on an XML layout?

01-18 17:32:05.672: E/AndroidRuntime(271): FATAL EXCEPTION: main
01-18 17:32:05.672: E/AndroidRuntime(271): java.lang.NullPointerException
01-18 17:32:05.672: E/AndroidRuntime(271):  at com.example.flybase2.alarmset.set(alarmset.java:32)
01-18 17:32:05.672: E/AndroidRuntime(271):  at com.example.flybase2.addAppointment.onClick(addAppointment.java:68)
01-18 17:32:05.672: E/AndroidRuntime(271):  at android.view.View.performClick(View.java:2408)
01-18 17:32:05.672: E/AndroidRuntime(271):  at android.widget.CompoundButton.performClick(CompoundButton.java:99)
01-18 17:32:05.672: E/AndroidRuntime(271):  at android.view.View$PerformClick.run(View.java:8816)
01-18 17:32:05.672: E/AndroidRuntime(271):  at android.os.Handler.handleCallback(Handler.java:587)
01-18 17:32:05.672: E/AndroidRuntime(271):  at android.os.Handler.dispatchMessage(Handler.java:92)
01-18 17:32:05.672: E/AndroidRuntime(271):  at android.os.Looper.loop(Looper.java:123)
01-18 17:32:05.672: E/AndroidRuntime(271):  at android.app.ActivityThread.main(ActivityThread.java:4627)
01-18 17:32:05.672: E/AndroidRuntime(271):  at java.lang.reflect.Method.invokeNative(Native Method)
01-18 17:32:05.672: E/AndroidRuntime(271):  at java.lang.reflect.Method.invoke(Method.java:521)
01-18 17:32:05.672: E/AndroidRuntime(271):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
01-18 17:32:05.672: E/AndroidRuntime(271):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
01-18 17:32:05.672: E/AndroidRuntime(271):  at dalvik.system.NativeStart.main(Native Method)

Heres my class that calls the 'alarmset' class:

package com.example.flybase2;

import java.sql.Date;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.Spinner;
import android.widget.TimePicker;
import android.widget.ToggleButton;

 public class addAppointment extends Activity implements OnClickListener {

Spinner typeSpinner;
Button bAddAppointment;
ToggleButton balarmButton;
TimePicker setTime;
DatePicker setDate;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.addappointment);



    Spinner typeSpinner = (Spinner) findViewById(R.id.spinner1);
    List<String> list = new ArrayList<String>();
    list.add("-");
    list.add("Medical");
    list.add("Business");
    list.add("Family");
    list.add("Other");
    ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list);
        dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    typeSpinner.setAdapter(dataAdapter);


    balarmButton = (ToggleButton) findViewById(R.id.toggleButton1);
    bAddAppointment = (Button) findViewById(R.id.btnAddAppointment);
    setTime = (TimePicker) findViewById(R.id.timePicker1);
    setDate = (DatePicker) findViewById(R.id.datePicker1);


    balarmButton.setOnClickListener(this);
    bAddAppointment.setOnClickListener(this);

}

@Override
public void onClick(View clickedOption) {
    switch(clickedOption.getId()){

    case (R.id.toggleButton1):

    Integer dobYear = setDate.getYear();
    Integer dobMonth = setDate.getMonth();
    Integer dobDate = setDate.getDayOfMonth();

    alarmset setDate = new alarmset(dobYear, dobMonth, dobDate);

    setDate.set();

    break;


    case (R.id.btnAddAppointment):  

    break;

    }

And the alarmset class:

package com.example.flybase2;

import java.sql.Date;
import android.app.AlarmManager;
public class alarmset {
    Integer yearSet;
    Integer monthSet;
    Integer dateSet;

    AlarmManager am;

    public alarmset(Integer dobYear, Integer dobMonth, Integer dobDate) {
        yearSet = dobYear;
        monthSet = dobMonth;
        dateSet = dobDate;
    }

    public void set() {
        Date set;
        set = new Date(yearSet - 1900, monthSet, dateSet);
        am.set(AlarmManager.RTC_WAKEUP, set.getTime(), null);   
    }
}

In your alarmset class, you have this line:

am.set(AlarmManager.RTC_WAKEUP, set.getTime(), null);

You never instantiate the variable am . It will always be null when you reach the line that I mentioned, so you will throw a NullPointerException . You need to instantiate it, either by passing in and assigning a value, or via new AlarmManager() or another related constructor.

Also, as a matter of style, class names should be capitalized. Eg. Alarmset or AlarmSet instead of alarmset.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM