1

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!!

  • possible duplicate of [Android: switching screens with new activity or just changing content view](http://stackoverflow.com/questions/6811989/android-switching-screens-with-new-activity-or-just-changing-content-view) – Andro Selva Aug 07 '13 at 04:16
  • I guess you got confused with threads and UI, there's only one MainThread/UIThread in Android. and you cannot set views for a BackgroundThread. – Lalith B Aug 07 '13 at 11:54

3 Answers3

3

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.

Sushil
  • 8,250
  • 3
  • 39
  • 71
0

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); 
            }
        });
user2652394
  • 1,686
  • 1
  • 13
  • 15
0

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!