0

我在我的应用程序中创建了 1 个后台服务,用于提醒会议。在这个应用程序中,我得到

02-15 15:29:19.251: E/AndroidRuntime(7878): FATAL EXCEPTION: Timer-0
02-15 15:29:19.251: E/AndroidRuntime(7878): java.lang.NullPointerException
02-15 15:29:19.251: E/AndroidRuntime(7878):at com.oceans.reminderApp.ReminderAppService.doServiceWork(ReminderAppService.java:100)
02-15 15:29:19.251: E/AndroidRuntime(7878): at com.oceans.reminderApp.ReminderAppService$1.run(ReminderAppService.java:78)02-15 15:29:19.251: E/AndroidRuntime(7878): at java.util.Timer$TimerImpl.run(Timer.java:284)02-15 15:29:37.473: I/Process(7878): Sending signal. PID: 7878 SIG: 9


02-15 15:29:57.663: E/AndroidRuntime(8029): FATAL EXCEPTION: Timer-0
02-15 15:29:57.663: E/AndroidRuntime(8029): java.lang.NullPointerException
02-15 15:29:57.663: E/AndroidRuntime(8029): at com.oceans.reminderApp.ReminderAppService.doServiceWork(ReminderAppService.java:100)
02-15 15:29:57.663: E/AndroidRuntime(8029): at com.oceans.reminderApp.ReminderAppService$1.run(ReminderAppService.java:78)
02-15 15:29:57.663: E/AndroidRuntime(8029): at java.util.Timer$TimerImpl.run(Timer.java:284)

这是我的服务..

public class ReminderAppService extends Service{

public static TestAdapter dbAdp;
public static FirstActivity MAIN_ACTIVITY;  

int nDay,nMonth,nYear,nHour,nMin;

private Timer timer=new Timer();   

private static long DELAY_INTERVAL = 0 ; 

private static long UPDATE_INTERVAL = 1 * 6 * 10000;
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy HH:mm");


public static void setMainActivity(FirstActivity activity)
{
  MAIN_ACTIVITY = activity; 
  dbAdp = new TestAdapter(activity.getApplicationContext());
}

public IBinder onBind(Intent intent) {
    return null;
}

@Override
public void onCreate() {
    // TODO Auto-generated method stub
    super.onCreate();
     _startService();
      if (MAIN_ACTIVITY != null)  Log.d(getClass().getSimpleName(), "Reminder App Service started");
}

@Override
public void onDestroy() {
    // TODO Auto-generated method stub
    super.onDestroy();
     _shutdownService();
     if (MAIN_ACTIVITY != null)  Log.d(getClass().getSimpleName(), "Reminder App Service stopped");
}

@Override
public void onStart(Intent intent, int startId) {
    // TODO Auto-generated method stub
    super.onStart(intent, startId);
}

 private void _startService()
    {     
        timer.scheduleAtFixedRate(   

              new TimerTask() {

                    public void run() {

                        try{

                            doServiceWork();

                            Thread.sleep(UPDATE_INTERVAL);

                        }catch(InterruptedException ie){

                            Log.e(getClass().getSimpleName(), "Reminder App Service InterruptedException"+ie.toString());
                        }

                    }
                  },
                  DELAY_INTERVAL,
                  UPDATE_INTERVAL);
    }


    /*
     * start the processing, the actual work, getting config params, get data from network etc
     */
    public void doServiceWork()
    {
        //Toast.makeText(getApplicationContext(), "helllllllllo", Toast.LENGTH_SHORT).show();
            dbAdp.open();
            getCalendar();
            checkMeetingReminder();
            dbAdp.close();

    }

    public void getCalendar(){
        Calendar cal = Calendar.getInstance();
        nDay = cal.get(Calendar.DATE);
        nMonth = cal.get(Calendar.MONTH);
        nYear = cal.get(Calendar.YEAR);
        nHour= cal.get(Calendar.HOUR_OF_DAY);
        nMin = cal.get(Calendar.MINUTE);
    }

    public void checkMeetingReminder(){
        Cursor meetCursor = dbAdp.meeting_queueAll();
        if(meetCursor.moveToFirst() && meetCursor.getCount() >0){
            do{
                try {

                    String mDate = meetCursor.getString(meetCursor.getColumnIndex(dbAdp.MEETING_REMINDER_DATETIME));
                    String mTag = meetCursor.getString(meetCursor.getColumnIndex(dbAdp.MEETING_TAG));
                    String mId = meetCursor.getString(meetCursor.getColumnIndex(dbAdp.MEETING_ID));
                    int snoozeStatus = meetCursor.getInt(meetCursor.getColumnIndex(dbAdp.SNOOZE_OFF));
                    java.util.Date meetDate = dateFormat.parse(mDate);
                    java.util.Date todayDate = new java.util.Date(nYear-1900,nMonth,nDay,nHour,nMin);

                    if (meetDate.equals(todayDate)){
                        Intent i = new Intent(ReminderAppService.this, ReminderAlertClass.class);
                        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        i.putExtra("MeetingId", mId);
                        i.putExtra("Message", "Time For Meeting " + mTag);
                        i.putExtra("MeetingDate",mDate);
                        startActivity(i);
                        Log.i("Meeting Reminder :", "Time for Meeting "+mTag);
                    }
                } catch (ParseException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    Log.e("Meeting Reminder Parse :", e.getMessage());
                } catch (TimeFormatException e){
                    Log.e("Meeting Reminder Time format :", e.getMessage());
                }

            }while(meetCursor.moveToNext());
        }
        meetCursor.close();
    }

    /*
     * shutting down the service
    */
    private void _shutdownService()
    {
      if (timer != null) timer.cancel();
    }
}

谁能告诉我错误在哪里,我该如何解决?任何形式的帮助将不胜感激。提前致谢。

4

1 回答 1

0

第一条评论是正确的,您使用的是空 dbAdp。你有一个静态函数来设置 dbAdp 并且你正在这样做以获取应用程序内容,我建议在 onCreate 函数中初始化 dbAdp,Service 也是一个上下文,所以你应该能够调用 this.getApplicationContext()

这不会回答您的问题,但可能会为您节省一些时间,我发现 Timer 在某些不遵守固定费率请求并一次拨打多个电话的 android 版本中存在缺陷。查看 AlarmManager 进行调度而不是使用服务:http: //developer.android.com/reference/android/app/AlarmManager.html

于 2013-02-15T11:18:14.907 回答