0

I have written application that combines widget, service syncronizing widget and activity for configuring widget.

I've started network transfer to server to fetch weather data and app crashed. After stack debugging I've got network in main thread exception.

After googlin some time, I've rewritten all network communication to AsyncTask but... running application gives me error

Only the original thread that created a view hierarchy can touch its views.

Now im stuck - data fetched by http client must be pushed into set of LinearLayouts and TextViews/ImageViews. 7 day weather gives me pretty big bunch of them - bigger than layout.xml can handle.

I'm asking - how to run HTTP sync in thread (as main thread network access gives me Exception) and sync views by View.add(object) in thread gives me:

> E/AndroidRuntime(965): FATAL EXCEPTION: AsyncTask #1
> E/AndroidRuntime(965): java.lang.RuntimeException: An error occured while executing doInBackground()
> E/AndroidRuntime(965):        at android.os.AsyncTask$3.done(AsyncTask.java:278)
> E/AndroidRuntime(965):        at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
> E/AndroidRuntime(965):        at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
> E/AndroidRuntime(965):        at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
> E/AndroidRuntime(965):        at java.util.concurrent.FutureTask.run(FutureTask.java:137)
> E/AndroidRuntime(965):        at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
> E/AndroidRuntime(965):        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
> E/AndroidRuntime(965):        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
> E/AndroidRuntime(965):        at java.lang.Thread.run(Thread.java:856) 
> E/AndroidRuntime(965): Caused by: android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.

Any ideas?

  • [The Stackoverflow search gives you 459 results for `NetworkOnMainThreadException`](http://stackoverflow.com/search?q=NetworkOnMainThreadException). I feel like this question gets asked every second day. – Ahmad May 17 '13 at 21:25
  • To be fair, the OP did say he researched the original problem of `NetworkONMainThreadException` and put all of the network stuff in `AsyncTask` but now has this other problem. This type of question is asked too often but at least in this case the OP has done some research and tried to solve the problem on his own – codeMagic May 17 '13 at 21:39
  • @codeMagic Oh I completely skipped the bottom part of the question. I just read `NetworkONMainThreadException` and turned off. Sorry Mateusz. – Ahmad May 17 '13 at 21:50

1 Answers1

1

I'm guessing you are trying to update the Views in doInBackground(). You need to do this in onPostExecute() or one of the other methods as doInBackground() doesn't run on the UI thread. You can also pass results back to an Activity method if you need to. If its not an inner class of your Activity then you can use the AsyncTask's constructor to receive params from the calling Activity if you need to pass it a context or other references

If you are already doing this then please post your AsyncTask so we can see what's going on.

codeMagic
  • 44,549
  • 13
  • 77
  • 93