Reputation: 33
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
Reputation: 33
Thanks for all!
I resolved the isuue using the next resolution:
I was trying to change the layout within thread and not through the handler!
Upvotes: 0
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
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