My problem stems from my countdown service class. The onTick method is throwing random null pointers. Random enough it's hard to catch but it crashes the app every time. Here is the code from the countdown service class
@Override
public void onCreate() {
super.onCreate();
stopNotify();
Log.i(TAG, "Starting timer...");
cdt = new CountDownTimer(900000, 1000) {
@Override
public void onTick(long millisUntilFinished) {
long millis = millisUntilFinished;
String hms = String.format("%02d:%02d",
TimeUnit.MILLISECONDS.toMinutes(millis) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis)),
TimeUnit.MILLISECONDS.toSeconds(millis) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis)));
MainActivity.tv.setText(hms);
Log.i(TAG, "Countdown seconds remaining: " + millisUntilFinished / 1000);
bi.putExtra("countdown", millisUntilFinished);
sendBroadcast(bi);
}
@Override
public void onFinish() {
Log.i(TAG, "Timer finished");
showNotification();
savepref();
Intent intent = new Intent(BroadcastService.this, MainActivity.class);
intent.putExtra("id1",id1);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
};
cdt.start();
}
@Override
public void onDestroy() {
cdt.cancel();
Log.i(TAG, "Timer cancelled");
super.onDestroy();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;
}
@Override
public IBinder onBind(Intent arg0) {
return null;
}
More code code!!
private BroadcastReceiver br = new
BroadcastReceiver()
{
@Override
public void onReceive(Context context, Intent intent) {
updateGUI(intent); // or whatever method used to
update your GUI fields
}
};
private void updateGUI(Intent intent) {
if (intent.getExtras() != null) {
long millisUntilFinished =
intent.getLongExtra("countdown", 0);
Log.i(TAG, "Countdown seconds remaining: " +
millisUntilFinished / 1000);
}
}