在终止的应用程序中设置警报管理器的 nullPointerException
nullPointerException for set Alarm Manger in killed application
我想通过警报管理器启动我的服务。我写了下面的代码。当我的应用程序处于后台或前台时,这段代码工作正常,但是当我终止我的应用程序(我的意思是从最近的应用程序中刷应用程序)时,程序会出错。我不知道我的问题是什么以及如何解决这个问题。
错误:
Caused by: java.lang.NullPointerException at com.company.test.helper.ServiceSchedular.startService(ServiceSchedular.java:74)
我有一个 class 从应用程序扩展如下:
public class KITILApplication extends Application{
private static Context context;
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
ACRA.init(this);
ORMDroidApplication.initialize(this);
KITILApplication.context=getApplicationContext();
}
public static Context getappContext(){
return KITILApplication.context;
}
}
我的 ServiceSchedularService 用于安排启动服务的警报如下:
public class ServiceSchedular {
//Called from my main activity
private ServiceSchedular() {
alarmManager = (AlarmManager) KITILApplication.getappContext().getSystemService(Context.ALARM_SERVICE);
}
private static void startService(Date startDate) {
uniqueIdForStart = (int) Calendar.getInstance().getTimeInMillis();
//PlEASE NOTICE: I checked my application via both of startintent(comment startIntent and startIntent)
Intent startIntent = new Intent(KITILApplication.getappContext(), LocationService.class);
//Intent startIntent = new Intent("com.company.test.START_SERVICE");
PendingIntent startPendingIntent = PendingIntent.getService(KITILApplication.getappContext(), uniqueIdForStart, startIntent, PendingIntent.FLAG_UPDATE_CURRENT);
//I get error from below line !! Line = 74
alarmManager.set(AlarmManager.RTC_WAKEUP, millisToStartDate, startPendingIntent);
stopService(startDate);
}
public static void checkNextPeriod() {
.
.
startService(new Date(millisDate));
}
}
我有一个名为 checkNextPeriod 的 broadcastReciever,在 checkNextPeriod 中,我调用了 startService 方法。
AndroidManifest.xml:
<service android:name=".Service.LocationService">
<intent-filter>
<action android:name="com.company.test.START_SERVICE"></action>
</intent-filter>
</service>
我不知道我的问题是关于 alarmManager 对象或 intent 对象或应用程序上下文或其他东西。请帮我。
您的问题在这里:
private ServiceSchedular() {
alarmManager = (AlarmManager) KITILApplication.getappContext().getSystemService(Context.ALARM_SERVICE);
}
不要在构造函数中执行此操作。需要的时候打getSystemService()
就可以了。
我想通过警报管理器启动我的服务。我写了下面的代码。当我的应用程序处于后台或前台时,这段代码工作正常,但是当我终止我的应用程序(我的意思是从最近的应用程序中刷应用程序)时,程序会出错。我不知道我的问题是什么以及如何解决这个问题。
错误:
Caused by: java.lang.NullPointerException at com.company.test.helper.ServiceSchedular.startService(ServiceSchedular.java:74)
我有一个 class 从应用程序扩展如下:
public class KITILApplication extends Application{
private static Context context;
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
ACRA.init(this);
ORMDroidApplication.initialize(this);
KITILApplication.context=getApplicationContext();
}
public static Context getappContext(){
return KITILApplication.context;
}
}
我的 ServiceSchedularService 用于安排启动服务的警报如下:
public class ServiceSchedular {
//Called from my main activity
private ServiceSchedular() {
alarmManager = (AlarmManager) KITILApplication.getappContext().getSystemService(Context.ALARM_SERVICE);
}
private static void startService(Date startDate) {
uniqueIdForStart = (int) Calendar.getInstance().getTimeInMillis();
//PlEASE NOTICE: I checked my application via both of startintent(comment startIntent and startIntent)
Intent startIntent = new Intent(KITILApplication.getappContext(), LocationService.class);
//Intent startIntent = new Intent("com.company.test.START_SERVICE");
PendingIntent startPendingIntent = PendingIntent.getService(KITILApplication.getappContext(), uniqueIdForStart, startIntent, PendingIntent.FLAG_UPDATE_CURRENT);
//I get error from below line !! Line = 74
alarmManager.set(AlarmManager.RTC_WAKEUP, millisToStartDate, startPendingIntent);
stopService(startDate);
}
public static void checkNextPeriod() {
.
.
startService(new Date(millisDate));
}
}
我有一个名为 checkNextPeriod 的 broadcastReciever,在 checkNextPeriod 中,我调用了 startService 方法。
AndroidManifest.xml:
<service android:name=".Service.LocationService">
<intent-filter>
<action android:name="com.company.test.START_SERVICE"></action>
</intent-filter>
</service>
我不知道我的问题是关于 alarmManager 对象或 intent 对象或应用程序上下文或其他东西。请帮我。
您的问题在这里:
private ServiceSchedular() {
alarmManager = (AlarmManager) KITILApplication.getappContext().getSystemService(Context.ALARM_SERVICE);
}
不要在构造函数中执行此操作。需要的时候打getSystemService()
就可以了。