我有一個廣播接收器,它根據pendingIntent調用服務。當活動處於onPause狀態時,服務工作正常,但是當調用應用程序的onDestroy時,服務正在給予NullPointer。當活動被破壞時來自Service的空指針異常
BaseActivity ---
@Override
protected void onPause() {
super.onPause();
isResume = false;
isPutToBackground = true;
isDataPutToBackground = true;
setNotification(_instance, false);
}
@Override
protected void onDestroy() {
super.onDestroy();
if (!isResume) {
isResume = false;
isPutToBackground = true;
isDataPutToBackground = true;
setNotification(_instance, true);
}}
public static void setNotification(Context ctx, boolean isDestroyed) {
cancelNotification(_instance);
myIntent = new Intent(ctx, NotificationReceiver.class);
dataIntent = new Intent(ctx, NotificationReceiver.class);
if (!Prefs.getInstance().isLoggedIn) {
createDailyReminder(ctx, false, false, isDestroyed);
} else {
if (Prefs.getInstance().isDataReminder) {
createWeeklyReminder(ctx, isDestroyed);
}
if (Prefs.getInstance().reminderInterval.equals("1 Day")|| Prefs.getInstance().reminderInterval.equals("")) {
createDailyReminder(ctx, true, false, isDestroyed);
} else {
createDailyReminder(ctx, true, true, isDestroyed);
}
}
}
public static void cancelNotification(Context ctx) {
Intent myIntent = new Intent(ctx, NotificationReceiver.class);
Intent dataIntent = new Intent(ctx, NotificationReceiver.class);
pendingIntent = PendingIntent.getBroadcast(ctx, 11122, myIntent, 0);
dataPendingIntent = PendingIntent.getBroadcast(ctx, 11133, dataIntent,0);
dataPendingIntent.cancel();
pendingIntent.cancel();
alreadyBackrogund = false;
dataAlreadyBackrogund = false;
}
private static void createWeeklyReminder(Context ctx, boolean isDestroyed) {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_WEEK, Calendar.TUESDAY);
calendar.set(Calendar.HOUR_OF_DAY, 10);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.AM_PM, Calendar.AM);
AlarmManager alarmManager = (AlarmManager) ctx.getSystemService(ALARM_SERVICE);
dataIntent.putExtra("DATA", true);
if (isDestroyed) {
dataIntent.putExtra("ISDESTROYED", true);
} else {
dataIntent.putExtra("ISDESTROYED", false);
}
dataPendingIntent = PendingIntent.getBroadcast(ctx, 11122, dataIntent,0);
alarmManager.setRepeating(AlarmManager.RTC, calendar.getTimeInMillis(),alarmManager.INTERVAL_DAY * 7, dataPendingIntent);
}
private static void createDailyReminder(Context ctx, boolean loggedIn,
boolean isWeekly, boolean isDestroyed) {
AlarmManager alarmManager = (AlarmManager) ctx
.getSystemService(ALARM_SERVICE);
if (loggedIn) {
if (!isWeekly) {
myIntent.putExtra("DATA", false);
if (isDestroyed) {
dataIntent.putExtra("ISDESTROYED", true);
} else {
dataIntent.putExtra("ISDESTROYED", false);
}
pendingIntent = PendingIntent.getBroadcast(ctx, 11133,
myIntent, 0);
alarmManager.setRepeating(AlarmManager.RTC,
getFuture10AMData(2), AlarmManager.INTERVAL_DAY,
pendingIntent);
} else {
myIntent.putExtra("DATA", false);
if (isDestroyed) {
dataIntent.putExtra("ISDESTROYED", true);
} else {
dataIntent.putExtra("ISDESTROYED", false);
}
pendingIntent = PendingIntent.getBroadcast(ctx, 11133,
myIntent, 0);
alarmManager.setRepeating(AlarmManager.RTC,
getFuture10AMData(8), AlarmManager.INTERVAL_DAY,
pendingIntent);
}
} else {
myIntent.putExtra("DATA", false);
if (isDestroyed) {
dataIntent.putExtra("ISDESTROYED", true);
} else {
dataIntent.putExtra("ISDESTROYED", false);
}
pendingIntent = PendingIntent.getBroadcast(ctx, 11133, myIntent, 0);
alarmManager.setRepeating(AlarmManager.RTC, getFuture10AMData(1),
AlarmManager.INTERVAL_DAY, pendingIntent);
}
}
private static long getFuture10AMData(int count) {
Calendar calendar = Calendar.getInstance();
// Date today = calendar.getTime();
calendar.add(Calendar.DAY_OF_YEAR, count);
// Date tomorrow = calendar.getTime();
calendar.set(Calendar.HOUR_OF_DAY, 10);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.AM_PM, Calendar.AM);
return calendar.getTimeInMillis();
}
的BroadcastReceiver
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
boolean isDataReminder = intent.getBooleanExtra("DATA", false);
boolean isDestroyed = intent.getBooleanExtra("ISDESTROYED", false);
Intent dataNotificationService = new Intent(context,
DataNotificationService.class);
Intent notificationService = new Intent(context,
NotificationService.class);
if (isDataReminder) {
dataNotificationService.putExtra("ISDESTROYED", isDestroyed);
context.startService(dataNotificationService);
} else {
notificationService.putExtra("ISDESTROYED", isDestroyed);
context.startService(notificationService);
}
}
服務代碼----
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
Prefs.getInstance().loadPrefs(getApplicationContext());
Log.i("MBB APP", "Data Service called");
boolean isDestroyed = intent.getBooleanExtra("ISDESTROYED", false);
System.out.println("Data Service Booleans"
+ BaseActivity.dataAlreadyBackrogund + ","
+ BaseActivity.isDataPutToBackground
+ " ISDestroyed boolean " + isDestroyed);
try {
if (BaseActivity.dataAlreadyBackrogund
&& BaseActivity.isDataPutToBackground) {
if (BaseActivity.isDataPutToBackground
&& Prefs.getInstance().isLoggedIn) {
generateDataNotification(
getApplicationContext(),
"You have created "
+ Prefs.getInstance().noOfQuotes
+ " and saved "
+ Prefs.getInstance().noOfLeads
+ " using MBB"
+ "\n"
+ "You last Quoted with MBB on "
+ Prefs.getInstance().lastQuoteTimeStamp
+ "\n"
+ "You last saved Lead with MBB on "
+ Prefs.getInstance().lastLeadTimeStamp
+ "\n"
+ "Add to your book of business now");
}
} else {
BaseActivity.dataAlreadyBackrogund = true;
// Prefs.getInstance().savePrefs(getApplicationContext());
}
} catch (Exception e) {
Log.e("MBB Service Exception", e.toString());
}
}
日誌----
09-03 15:42:04.008: E/AndroidRuntime(10701): FATAL EXCEPTION: main
09-03 15:42:04.008: E/AndroidRuntime(10701): java.lang.RuntimeException: Unable to start service [email protected] with null: java.lang.NullPointerException
09-03 15:42:04.008: E/AndroidRuntime(10701): at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2571)
09-03 15:42:04.008: E/AndroidRuntime(10701): at android.app.ActivityThread.access$2000(ActivityThread.java:140)
09-03 15:42:04.008: E/AndroidRuntime(10701): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1334)
09-03 15:42:04.008: E/AndroidRuntime(10701): at android.os.Handler.dispatchMessage(Handler.java:99)
09-03 15:42:04.008: E/AndroidRuntime(10701): at android.os.Looper.loop(Looper.java:137)
09-03 15:42:04.008: E/AndroidRuntime(10701): at android.app.ActivityThread.main(ActivityThread.java:4921)
09-03 15:42:04.008: E/AndroidRuntime(10701): at java.lang.reflect.Method.invokeNative(Native Method)
09-03 15:42:04.008: E/AndroidRuntime(10701): at java.lang.reflect.Method.invoke(Method.java:511)
09-03 15:42:04.008: E/AndroidRuntime(10701): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
09-03 15:42:04.008: E/AndroidRuntime(10701): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
09-03 15:42:04.008: E/AndroidRuntime(10701): at dalvik.system.NativeStart.main(Native Method)
是完整的日誌貓嗎? – 2014-09-02 07:15:40
y,請延長日誌貓... – RusArtM 2014-09-02 07:30:16
它幾乎是完整的日誌。 – 2014-09-02 07:40:14