我在我的应用程序中创建了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();
}
}
任何人都可以告诉我错误在哪里,我该如何解决? 任何形式的帮助将不胜感激。 提前谢谢。
答案 0 :(得分:0)
第一条评论是正确的,您使用的是null dbAdp。 你有一个静态函数来设置dbAdp而你正在这样做以获取应用程序内容,我建议在onCreate函数中初始化dbAdp,Service也是一个Context,所以你应该能够调用this.getApplicationContext()
这不会回答你的问题,但可能会节省你一些时间,我发现Timer在某些Android版本中有错误,不遵守固定费率请求并立即拨打许多电话。检查AlarmManager以进行调度而不是使用服务: http://developer.android.com/reference/android/app/AlarmManager.html