0

I have a TimerTask in a library module I use, I have access to the source code and it worked independantly, however when I call the method startPoll()from the main application i get the following error, SensorPoll is the TimerTask:

E/AndroidRuntime: FATAL EXCEPTION: Timer-0
    Process: com.app, PID: 29465
    java.lang.ExceptionInInitializerError
        at com.app.Utilities.SensorPoll.run(SensorPoll.java:55)
        at java.util.TimerThread.mainLoop(Timer.java:562)
        at java.util.TimerThread.run(Timer.java:512)
     Caused by: java.lang.RuntimeException: Can't create handler inside thread Thread[Timer-0,5,main] that has not called Looper.prepare()
        at android.os.Handler.<init>(Handler.java:207)
        at android.os.Handler.<init>(Handler.java:119)
        at com.app.MainActivity.<clinit>(MainActivity.java:31)
        at com.ap.Utilities.SensorPoll.run(SensorPoll.java:55) 
        at java.util.TimerThread.mainLoop(Timer.java:562) 
        at java.util.TimerThread.run(Timer.java:512) 

The error is on the public void run(){ line in the TimerTask.

I need to be able to run this, i don't try to call it from an AsyncTask, just from inside onCreateView in a fragment. It's actaully called from within the onFinish() of a CountDownTimer(). How can i avoid this error and start my TimerTask on it's Thread? If you need anymore info please ask.

The Code: The Call

        new CountDownTimer(10000, 1000){

            @Override
            public void onTick(long millisUntilFinished) {
                countdown.setText(String.valueOf(String.valueOf(counter)));
                counter--;
            }


            @RequiresApi(api = Build.VERSION_CODES.O)
            @Override
            public void onFinish() {
                countdown.setText("Initialised");
                srace.startSrace();
                
            }
        }.start();

The Method which calls the PollDirectly:

    public void startSrace(){
     
                    sraceManager.startPoll();

    }

The startPoll Method:(We are now in the Module Library)

    public static void startPoll() {
        TimerTask sensorPoll = new SensorPoll();
        Timer timer = new Timer();
        timer.scheduleAtFixedRate(sensorPoll, 0L, 9L);
    }

SensorPoll - The TimerTask

   public SensorPoll() {
    }

    @RequiresApi(
        api = 24
    )
//This is the error line-->
public void run() {
    //Just gets sensor info in here

    }

Not much too it. Thanks

HTBuild
  • 53
  • 6

0 Answers0