raju
raju

Reputation: 785

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

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)

Upvotes: 2

Views: 12840

Answers (7)

user2197563
user2197563

Reputation: 1

Do it like this: instead of using asynktask use a thread. Here it goes:

thread = new Thread() 
{
  @Override
  public void run() 
  {
    try
    {
      synchronized(this) 
      {
  runOnUiThread(new Runnable() 
  { 
           @Override
           public void run() 
           {                        
            your code goes here
           }
       });                  
     }
  }
  catch(Exception e)
  {
     e.printStackTrace();
  }         
 };
};
thread.start();    

Upvotes: -1

Md Abdul Gafur
Md Abdul Gafur

Reputation: 6201

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.

Upvotes: 1

vinothp
vinothp

Reputation: 10059

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().

Upvotes: 11

Hardik4560
Hardik4560

Reputation: 3220

You have to the portion to the UI thread or you can use runOnUIThread method which is available from context

Upvotes: 1

user
user

Reputation: 87064

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.

Upvotes: 3

Vipul
Vipul

Reputation: 28093

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

Upvotes: 1

Blundell
Blundell

Reputation: 76458

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

Upvotes: 3

Related Questions