Problem: Android : CalledFromWrongThreadException;: Only the original thread that created a view hierarchy can touch its views

Avoid performing long-running operations (such as network I/O) directly in the UI thread — the main thread of an application where the UI is run — or your application may be blocked and become unresponsive. Here is a brief summary of the recommended approach for handling expensive operations:
  1. Create a Handler object in your UI thread
  2. Spawn off worker threads to perform any required expensive operations
  3. Post results from a worker thread back to the UI thread's handler either through a Runnable or a Message
  4. Update the views on the UI thread as needed

AsyncTask enables proper and easy use of the UI thread. This class allows to perform background operations and publish results on the UI thread without having to manipulate threads and/or handlers.

So Here is the best solutions:

ou have to move the portion of the background task that updates the ui onto the main thread. There is a simple piece of code for this:
runOnUiThread(new Runnable() {
public void run() {

//stuff that updates ui


Documentation for Activity.runOnUiThread.

or you may use this:

Basically you would wrap //do whatever you wantin a Runnable and invoke it with a Handler instance.

Handler refresh = new Handler(Looper.getMainLooper());
.post(new Runnable() {
public void run()
//do whatever you want


or simple do that:

you don't call directly the onProgressUpdate, you have to call publishProgress and let the AsynTask framework to handle the onProgressUpdate to be called back on the UI thread.

Finally, google it : Here is the best solutions that reach there. Gooogled

Happy Coding!!!

No comments:

Post a Comment


Most recent Elastic Search tutorials for beginner, intermediate and advance level developer, Yubraj Pokharel, in CodingWorkspace