2014-08-28 76 views
-4

我有一個廣播接收器,它根據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) 
+1

是完整的日誌貓嗎? – 2014-09-02 07:15:40

+0

y,請延長日誌貓... – RusArtM 2014-09-02 07:30:16

+0

它幾乎是完整的日誌。 – 2014-09-02 07:40:14

回答

0

我建議你把你的代碼到另一個地方。 從官方網站:

注意:不要指望這種方法被稱爲保存數據的地方!例如,如果一個活動正在編輯內容提供者中的數據,那麼這些編輯應該在onPause()或onSaveInstanceState(Bundle)中提交,而不是在這裏。這種方法通常被實現爲釋放資源,例如與活動相關聯的線程,這樣一個被銷燬的活動就不會離開這些東西,而其他應用程序仍在運行。在某些情況下,系統只會在不調用該方法(或任何其他方法)的情況下終止該活動的託管過程,因此不應該將其用於在過程消失後執行那些旨在保留的事情。

派生類必須調用超類的此方法的實現。如果他們不這樣做,就會拋出異常。 :http://developer.android.com/reference/android/app/Activity.html#onDestroy%28%29

0

我想你應該叫super.onDestroy()當你與你當地的清理做:

@Override 
protected void onDestroy() { 
    if (!isResume) { 
     isResume = false; 
     isPutToBackground = true; 
     isDataPutToBackground = true; 
     setNotification(_instance, true); 
    } 
    super.onDestroy(); 
}