报警管理器日期问题

时间:2013-01-18 18:12:50

标签: java android

我正在尝试在指定日期设置一个警报管理器类(由日期选择器设置),在指定日期将向用户显示一个toast样式消息,基本上告诉他们他们的约会是那天。< / p>

到目前为止,我有一个切换按钮,它调用一个单独的类来设置警报管理器,其中包含发送给构造函数的日期,月份和年份变量。

目前我收到以下错误:

我是否需要设置活动意图以在XML布局上显示消息?

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)

这是我的班级,称为“闹钟”类:

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;

    }

闹钟类:

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);   
    }
}

1 个答案:

答案 0 :(得分:0)

在你的闹钟类中,你有这一行:

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

你永远不会实例化变量am当你到达我提到的那一行时它总是为空,所以你会抛出一个NullPointerException。您需要通过传入和分配值,或通过new AlarmManager()或其他相关构造函数来实例化它。

另外,作为一种风格,类名应该大写。例如。 Alarmset或AlarmSet而不是alarmset。