我有一个充满警报的数据库,创建警报的服务在启动时启动,但是在循环中崩溃一次以创建警报。错误是:
" java.lang.NullPointerException:尝试调用虚方法 ' java.lang.String android.content.Context.getPackageName()'在null 对象参考"
编辑:
代码:
public class NotificationServiceStartOnBoot extends Service {
UserDbHelper userDbHelper;
SQLiteDatabase sqLiteDatabase;
Context context;
Cursor cursor;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
Log.e("SERVICE", "STARTED...");
userDbHelper = new UserDbHelper(getApplicationContext());
sqLiteDatabase = userDbHelper.getReadableDatabase();
cursor = userDbHelper.getNotifications(sqLiteDatabase);
Log.e("SERVICE", "About to begin loop...");
if(cursor.moveToFirst())
{
Log.e("SERVICE", "if statement accepted...");
do
{
Log.e("SERVICE", "notification loop begun...");
String date,UID;
date = cursor.getString(0);
UID = cursor.getString(1);
// Long notificationDate = Long.valueOf(date);
Long notificationUID = Long.valueOf(UID);
long milliSeconds= Long.parseLong(date);
//System.out.println(milliSeconds);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(milliSeconds);
final int _id = (int) System.currentTimeMillis();
Intent alarmIntent = new Intent(context, AlarmReceiver.class);
alarmIntent.putExtra("nID", notificationUID);
//make sure the intent have id's
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, _id, alarmIntent , 0);
AlarmManager alarmManager = (AlarmManager)context.getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC, calendar.getTimeInMillis(), pendingIntent);
}while (cursor.moveToNext());
}
Log.e("SERVICE", "outside if statement...");
//NotificationGenerator.generateNotification(context, null, null, null, null);
}
}
答案 0 :(得分:0)
您的上下文为空。 Context context;
您在哪里设置上下文?在使用之前设置您的上下文。