-1

I'm getting a E/Error: java.lang.NullPointerException when running Android Timer.

How do I set current activity to run on the UI thread?

Full ERROR Message:

E/Error: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.Activity.runOnUiThread(java.lang.Runnable)' on a null object reference
E/AndroidRuntime: FATAL EXCEPTION: Timer-0

This is my code, see comment below to understand where the exception happens:

            //set a new Timer

            timer = new Timer();
            initializeTimerTask(webView);

            //schedule the timer, after the first 5000ms the TimerTask will run every 10000ms
            timer.schedule(timerTask, 10000, 10000); //
        }
    });

    return root;
}

public void initializeTimerTask(final WebView wv) {

// HERE IS WHERE THE NULL REFERENCE ERROR OCCURS (on context):

    final Activity context = this.context;

    timerTask = new TimerTask() {
        @Override
        public void run() {
            context.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    wv.reload();
                    new SoapCall().execute();
                }
            });
        }
    };
}
CraigJSte
  • 912
  • 6
  • 17
  • 33

1 Answers1

0

You can always call this to run something on the UI thread:

(new Handler(Looper.getMainLooper())).post(new Runnable() {
    @Override
    public void run() {

    }
});
Chris
  • 1,180
  • 1
  • 9
  • 17