Moritz
Moritz

Reputation: 159

Timer stops app

In my app, I have a timer. But when I make for example a toast inside the timer the app crashes. But when I only increase a variable the timer works.

Here is my code:

package com.dev.moritz.colora;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.InputType;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.Toast;

import java.util.Timer;
import java.util.TimerTask;

public class gameActivity extends AppCompatActivity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
        WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(R.layout.activity_game);

        startGame();
    }

public void startGame() {
        Timer ballMovement = new Timer();
        ballMovement.scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {

                Toast.makeText(getApplicationContext(), "Round", Toast.LENGTH_SHORT).show();

            }
        }, 0, 5000);
    }

}

I hope anyone can help me. Moritz

Upvotes: 0

Views: 94

Answers (5)

Chintan Soni
Chintan Soni

Reputation: 25287

Reason: Any task running on Non-UI thread, cannot interact with the UI.

So, when you want to update UI from Timer Task, one way of the way is to use runOnUiThread() that comes from Activity class, put that piece of code using inside your TimerTask's run() method:

runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(gameActivity.this, "Round", Toast.LENGTH_SHORT).show();
            }
        });

And it will work as expected.

Upvotes: 2

Chintan Joshi
Chintan Joshi

Reputation: 1307

Exception: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()

This is an option that you can try.

Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
   private Handler updateUI = new Handler() {
       @Override
      public void dispatchMessage(Message msg) {
           super.dispatchMessage(msg);
           Toast.makeText(MainActivity.this, "142", Toast.LENGTH_SHORT).show();
       }
   };
   public void run() {
       try {
           updateUI.sendEmptyMessage(0);
       } catch (Exception e) { e.printStackTrace(); }
   }
}, 0, 5000); // time in milliseconds

Upvotes: 0

Nikhil Lotke
Nikhil Lotke

Reputation: 665

I tried at my end and here it is working fine:

You must have got RuntimeException: Can't create handler inside thread that has not called Looper.prepare()

To overcome above issue change your method to:

public void startGame() {
    Timer ballMovement = new Timer();
    ballMovement.scheduleAtFixedRate(new TimerTask() {
        @Override
        public void run() {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(Main2Activity.this, "Round", Toast.LENGTH_SHORT).show();
                }
            });
        }
    }, 0, 5000);
}

Upvotes: -2

Jenish
Jenish

Reputation: 234

Try below code with runOnUiThread ,

runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(getApplicationContext(), "Round", Toast.LENGTH_SHORT).show();
            }
        });

Upvotes: 0

Thomas Mary
Thomas Mary

Reputation: 1569

Try this :

public void startGame() {
    Timer ballMovement = new Timer();
    ballMovement.scheduleAtFixedRate(new TimerTask() {
        @Override
        public void run() {

            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(getApplicationContext(), "Round", Toast.LENGTH_SHORT).show();
                }
            });
        }
    }, 0, 5000);
}

Upvotes: 1

Related Questions