Question

I am developing a simple game using SurfaceView. I made layout containing some buttons. I also made a function to show a popup window, with that layout, which is used to shift between game states when certain criteria are met. The onDraw method is constantly called and draws some bitmaps on the canvas. When the criterion is met, the popup window function is called.

I have tested the function before the game thread stated and it works nicely but when I call it inside of the onDraw method, the app crashes and throws the following error in the log: Can't create handler inside thread that has not called Looper.prepare().

How to solve it please?

Was it helpful?

Solution

I solved the problem using handler as follows:

    final Handler handler2 = new Handler();
Runnable runnable2 = new Runnable() {
    @Override
    public void run() {
        Main.initLetterSetsPop();
    }
};

where initLetterSetsPop() is the function in the Main activity to show the popup window. When I want to display the window in the onDraw() method, I simply use handler2.post(runnable2);

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top