Javier Bianco
Javier Bianco

Reputation: 33

Change Layout when Thread finish

I want to change the layout when a thread ends, but I don't understand the bug; for example:

res/layout:

-mainView.xml

-threadView.xml

MainActivity.java

protected void firstThread() {

    setContentView(R.layout.threadView);        

    firstThread = new Thread(new Runnable() {
        @Override
        public void run() {
            SystemClock.sleep(7000);

            setContentView(R.layout.threadView);        
        }           
    });
    firstThread.start();          
}


@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);

    firstThread();
}

Thanks for all!!

Upvotes: 1

Views: 351

Answers (3)

Javier Bianco
Javier Bianco

Reputation: 33

Thanks for all!

I resolved the isuue using the next resolution:

http://inphamousdevelopment.wordpress.com/2010/10/11/using-a-viewswitcher-in-your-android-xml-layouts/

I was trying to change the layout within thread and not through the handler!

Upvotes: 0

Sushil
Sushil

Reputation: 8488

You cannot modify UI thread from any other thread. Either you can post a message from your other thread and write a handler in UI thread to change the layout. Try using Asynctask. your life will be simpler.

Upvotes: 3

user2652394
user2652394

Reputation: 1686

At least you should post the error stack for everyone to see your problem. Assume there is a problem, I guess it is because you cant modify the UI in another thread. Try inside your thread:

        runOnUiThread(new Runnable() {

            @Override
            public void run() {
                setContentView(R.layout.threadView); 
            }
        });

Upvotes: 0

Related Questions