2013-01-18 107 views
0

我想在指定的日期(由日期選擇器設置)設置一個報警管理器類,在指定的日期基本告訴他們他們的約會是那天。報警管理器日期問題

到目前爲止,我有一個切換按鈕,它調用一個單獨的類來設置發送給構造函數的日期,月份和年份變量的鬧鐘管理器。

在我收到以下錯誤的時刻:

我是否需要設置一個活動的意圖,以顯示對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) 

我的繼承人類調用 'alarmset' 類:

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

'com.example.flybase2.alarmset.set(alarmset.java:32)處的java.lang.NullPointerException' –

回答

0

在你alarmset類,你有這樣一行:

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

你永遠不會實例化變量am當你到達我提到的那條線時,它將永遠是空的,所以你會拋出一個NullPointerException。您需要通過傳入和分配值來實例化它,或者通過new AlarmManager()或其他相關的構造函數來實例化它。

另外,就風格而言,類名應該大寫。例如。 Alarmset或AlarmSet,而不是報警設置。