Use Thread control Android UI
android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:4637)
at android.view.ViewRootImpl.invalidateChildInParent(ViewRootImpl.java:876)
at android.view.ViewGroup.invalidateChild(ViewGroup.java:4174)
at android.view.View.invalidate(View.java:10313)// run on ui thread
runOnUiThread(new Runnable() {
public void run() {
// TODO
}
});// Start thread
@Override
protected void onResume()
{
super.onResume();
myThread.start();
}
// Stop thread
@Override
public void onPause()
{
super.onPause();
myThread.interrupt();
}
// Execute thread after 5s.
final Thread myThread = new Thread() {
public void run() {
try {
this.sleep(5000);
// run on ui thread
runOnUiThread(new Runnable() {
public void run() {
// TODO
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
};Last updated