Q: Android app forced to close on background colour change? so I am building an experiment app where the background will change colour at random intervals.
I am stuck on the background change.
I have working code that changes the background colour, but when I put it in a thread/ try and catch bracket, the application is forced to close and doesnt give me an error?
Here is the code that works when used in the oncreate method:
View view = this.getWindow().getDecorView();
view.setBackgroundColor(Color.RED);

But when I want to make it "sleep" for 1 second and then change it to red, it bombs out.
Please note that this method is a separate method from the oncreate and is called from within there and will not work for some reason?
public void changeBackground(final View v){
Thread timer = new Thread(){
    public void run(){
        try{
            sleep(1000);
        }catch (InterruptedException e) {
            e.printStackTrace();
        }finally{
            v.setBackgroundColor(Color.RED);
        }
    }
};
timer.start();
}

What am I doing wrong?
What I need:
when the app starts, it must wait for 1 second and then change the background colour without it bombing out.
Thanks in advance!

A: UI operations can't be done in a Thread
 v.setBackgroundColor(Color.RED); 

Remove above line from finally and Use a runOnUIthread() to update UI in Finally.
and your final code will look like this
public void changeBackground(final View v){
        Thread timer = new Thread(){
            public void run(){
                try{
                    sleep(1000);
                }catch (InterruptedException e) {
                    e.printStackTrace();
                }finally{

                    runOnUiThread(new Runnable() {

                        @Override
                        public void run() {
                            // TODO Auto-generated method stub
                              v.setBackgroundColor(Color.RED);
                        }
                    });

                }
            }
        };
        timer.start();
        }


A: Manipulate view state on UI thread:
v.post(new Runnable() {
    @Override
    public void run() {
        v.setBackgroundColor(Color.RED);
    }
});

More about it: http://developer.android.com/guide/components/processes-and-threads.html

A: You cannot access UI thread from your custom thread. You have to run your runnable in UI thread. Change your changeBackground method to the following, 
    public void changeBackground(final View v) {
    Thread timer = new Thread() {
        public void run() {
            try {
                sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                v.setBackgroundColor(Color.RED);
            }
        }
    };
    this.runOnUiThread(timer);
}

Or you can use Asynctask, this handles that issue for you. Here, and here.

A: Solutions to fix this issue:-


*

*Update you App

*Clear Gmail Storage Data

*Clear App Cache and Data

*Reset App Preferences

*Reset Smartphone Factory Settings


I found these steps with the help of YouTube. 
Here is that link:-
youtube.com/watch?v=fx8Fv8RXag8
