error in asynctask : Only the original thread that created a view hierarchy can touch its views

12,814

Solution 1

I think you using some thing like
Toast.makeText(this, "Text", Toast.LENGTH_SHORT).show();
or using Views inside the doInBackground() method.

If you want to use those use it in onPostExecute().

Solution 2

You're modifying your Views from the method doInBackground which runs on another thread. In android this is forbidden, instead you should modify the views from the onPostExecute method only.

Solution 3

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

You cannot change the UI from a background thread.

The UI is run on the main thread, your ASyncTask is run on it's own thread.

If you want to notify the UI of a change from another Thread you will have to use handlers.

Take a look at :

https://stackoverflow.com/a/5185155/413127

or the runOnUiThread method

Solution 4

You have to move the portion of the background task that updates the ui onto the main thread.

Solution 5

This line make error

E/AndroidRuntime(1086): Caused by:Only the original thread that created 
a view hierarchy can touch its views".

May be you should try and show Asyntack doInBackGround() method.

Share:
12,814
raju
Author by

raju

I am an Android developer

Updated on June 04, 2022

Comments

  • raju
    raju almost 2 years
    06-18 16:50:24.733: W/dalvikvm(1086): threadid=11: thread exiting with uncaught exception (group=0x40015560)
    06-18 16:50:24.815: E/AndroidRuntime(1086): FATAL EXCEPTION: AsyncTask #3
    06-18 16:50:24.815: E/AndroidRuntime(1086): java.lang.RuntimeException: An error occured while executing doInBackground()
    06-18 16:50:24.815: E/AndroidRuntime(1086):     at android.os.AsyncTask$3.done(AsyncTask.java:200)
    06-18 16:50:24.815: E/AndroidRuntime(1086):     at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
    06-18 16:50:24.815: E/AndroidRuntime(1086):     at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
    06-18 16:50:24.815: E/AndroidRuntime(1086):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
    06-18 16:50:24.815: E/AndroidRuntime(1086):     at java.util.concurrent.FutureTask.run(FutureTask.java:138)
    06-18 16:50:24.815: E/AndroidRuntime(1086):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
    06-18 16:50:24.815: E/AndroidRuntime(1086):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
    06-18 16:50:24.815: E/AndroidRuntime(1086):     at java.lang.Thread.run(Thread.java:1019)
    06-18 16:50:24.815: E/AndroidRuntime(1086): Caused by: android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
    06-18 16:50:24.815: E/AndroidRuntime(1086):     at android.view.ViewRoot.checkThread(ViewRoot.java:2932)
    06-18 16:50:24.815: E/AndroidRuntime(1086):     at android.view.ViewRoot.requestLayout(ViewRoot.java:629)
    06-18 16:50:24.815: E/AndroidRuntime(1086):     at android.view.View.requestLayout(View.java:8267)
    06-18 16:50:24.815: E/AndroidRuntime(1086):     at android.view.View.requestLayout(View.java:8267)
    06-18 16:50:24.815: E/AndroidRuntime(1086):     at android.view.View.requestLayout(View.java:8267)
    06-18 16:50:24.815: E/AndroidRuntime(1086):     at android.view.View.requestLayout(View.java:8267)
    06-18 16:50:24.815: E/AndroidRuntime(1086):     at android.widget.RelativeLayout.requestLayout(RelativeLayout.java:257)
    06-18 16:50:24.815: E/AndroidRuntime(1086):     at android.view.View.requestLayout(View.java:8267)
    06-18 16:50:24.815: E/AndroidRuntime(1086):     at android.widget.AbsListView.requestLayout(AbsListView.java:1102)
    06-18 16:50:24.815: E/AndroidRuntime(1086):     at android.view.View.setFlags(View.java:4641)
    06-18 16:50:24.815: E/AndroidRuntime(1086):     at android.view.View.setVisibility(View.java:3116)
    06-18 16:50:24.815: E/AndroidRuntime(1086):     at com.mythrii.tripview.BusFrom$DownloadImageTask.doInBackground(BusFrom.java:274)
    06-18 16:50:24.815: E/AndroidRuntime(1086):     at com.mythrii.tripview.BusFrom$DownloadImageTask.doInBackground(BusFrom.java:1)
    06-18 16:50:24.815: E/AndroidRuntime(1086):     at android.os.AsyncTask$2.call(AsyncTask.java:185)
    06-18 16:50:24.815: E/AndroidRuntime(1086):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
    06-18 16:50:24.815: E/AndroidRuntime(1086):     ... 4 more
    06-18 16:50:30.571: E/WindowManager(1086): Activity com.mythrii.tripview.BusFrom has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@4062bcc0 that was originally added here
    06-18 16:50:30.571: E/WindowManager(1086): android.view.WindowLeaked: Activity com.mythrii.tripview.BusFrom has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@4062bcc0 that was originally added here
    06-18 16:50:30.571: E/WindowManager(1086):  at android.view.ViewRoot.<init>(ViewRoot.java:258)
    06-18 16:50:30.571: E/WindowManager(1086):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:148)
    06-18 16:50:30.571: E/WindowManager(1086):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
    06-18 16:50:30.571: E/WindowManager(1086):  at android.view.Window$LocalWindowManager.addView(Window.java:424)
    06-18 16:50:30.571: E/WindowManager(1086):  at android.app.Dialog.show(Dialog.java:241)
    06-18 16:50:30.571: E/WindowManager(1086):  at android.app.ProgressDialog.show(ProgressDialog.java:107)
    06-18 16:50:30.571: E/WindowManager(1086):  at android.app.ProgressDialog.show(ProgressDialog.java:90)
    06-18 16:50:30.571: E/WindowManager(1086):  at android.app.ProgressDialog.show(ProgressDialog.java:85)
    06-18 16:50:30.571: E/WindowManager(1086):  at com.mythrii.tripview.BusFrom.onCreate(BusFrom.java:109)
    06-18 16:50:30.571: E/WindowManager(1086):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
    06-18 16:50:30.571: E/WindowManager(1086):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
    06-18 16:50:30.571: E/WindowManager(1086):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
    06-18 16:50:30.571: E/WindowManager(1086):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
    06-18 16:50:30.571: E/WindowManager(1086):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
    06-18 16:50:30.571: E/WindowManager(1086):  at android.os.Handler.dispatchMessage(Handler.java:99)
    06-18 16:50:30.571: E/WindowManager(1086):  at android.os.Looper.loop(Looper.java:130)
    06-18 16:50:30.571: E/WindowManager(1086):  at android.app.ActivityThread.main(ActivityThread.java:3683)
    06-18 16:50:30.571: E/WindowManager(1086):  at java.lang.reflect.Method.invokeNative(Native Method)
    06-18 16:50:30.571: E/WindowManager(1086):  at java.lang.reflect.Method.invoke(Method.java:507)
    06-18 16:50:30.571: E/WindowManager(1086):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
    06-18 16:50:30.571: E/WindowManager(1086):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
    06-18 16:50:30.571: E/WindowManager(1086):  at dalvik.system.NativeStart.main(Native Method)
    
  • raju
    raju almost 12 years
    thanks for ur reply how to handle exception in doInBackgroound method i already placed try catch block but it terminating the app
  • user
    user almost 12 years
    @raju Nobody can help you if you don't post some code. The basic ideea is that you can't modify the Views from doInBackground(like textView.setText). After you complete the task and the doInBackground method returns the method onPostExecute will be executed and there you should modify the views with the new data.
  • raju
    raju almost 12 years
    thank u alot i got solution from your reply its working for me
  • vinothp
    vinothp almost 12 years
    Cool..No problems.. I am glad that you resolved your problem.. If my answer helps you please mark it as answer...thanks