0

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

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

目前我收到以下错误:

我是否需要设置活动意图以在 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;

    }

还有alarmset类:

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

1 回答 1

0

在您的 alarmset 类中,您有以下行:

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

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

此外,作为风格问题,类名应该大写。例如。Alarmset 或 AlarmSet 而不是 alarmset。

于 2013-01-18T19:55:11.360 回答