indira
indira

Reputation: 6687

Android Thread makes the UI blank screen

I have written a Thread in my application. When the thread starts running, the UI screen becomes blank. How to can I avoid this?

public class SetTickerText extends Thread
{
    @Override
    public void run()
    {
        while(true)
        {
            SystemClock.sleep(25000);
            Log.i("Map", "after wait");
        }
    }

Upvotes: 2

Views: 1079

Answers (3)

Achintha Isuru
Achintha Isuru

Reputation: 3307

For me, the problem was that I have called

thread.run()

instead of

thread.start()

by mistake.

For more details follow,

https://beginnersbook.com/2015/03/why-dont-we-call-run-method-directly-why-call-start-method/

Upvotes: 0

forsvarir
forsvarir

Reputation: 10839

It's going to be something like (don't have a compiler near me):

new SetTickerText().start()

Essentially, when you tell the thread object to start, it spins up the new thread, which then invokes run for you. What you're doing is calling run from the UI thread, just like any other function, so it's blocking your UI thread from returning

Upvotes: 3

Moss
Moss

Reputation: 6012

You main problem is using SystemClock.sleep. This method will force the CPU going into deep thead. What you want to do is to sleep the current thread which is done using Thread.sleep: http://developer.android.com/reference/java/lang/Thread.html#sleep(long).

Upvotes: 1

Related Questions