0

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);
             }
          }
apk legion
  • 21
  • 1
  • 10

1 Answers1

0

Though, I don't know about rest of your code, but apparently it looks like the problem lies in this line

MainActivity.tv.setText(hms);

I guess, "tv" represents a text view, which you have taken in a static variable in MainActivity. This becomes null..

Debanjan
  • 2,817
  • 2
  • 24
  • 43
  • Yeah it's a textview. Anyway to keep it from going null? The service updates the textview – apk legion Jul 21 '17 at 10:14
  • You should not use any static reference to views. You can bind the service and MainActivity. Here is an example- https://stackoverflow.com/questions/14695537/android-update-activity-ui-from-service Or you can send Broadcast from the service, and register the activity as a broadcast receiver. – Debanjan Jul 21 '17 at 10:36
  • Ok so I would bind the service to the main activity and have it update the text view? – apk legion Jul 21 '17 at 10:45
  • Exactly. You can also use broadcast receiver. – Debanjan Jul 21 '17 at 10:55
  • ok still having trouble with this the example isnt really helping and theres alot of extra stuff in it. im not sure how to set the textview – apk legion Jul 22 '17 at 14:59
  • Another solution(dumb one), you can place "MainActivity.tv.setText(hms);" in a try catch block. try{ MainActivity.tv.setText(hms);}catch(Exception e){} Later, study about broadcast receivers, look other answers and change your code. – Debanjan Jul 24 '17 at 08:09
  • I'm already using a broadcast receiver I'll update the post with that code as well. – apk legion Jul 24 '17 at 10:09
  • Did you send your broadcast? LocalBroadcastManager.getInstance(this).sendBroadcast(callbackIntent); And registered and unregister in the activity? – Debanjan Jul 25 '17 at 07:38