Aviran
Aviran

Reputation: 5458

Android AsyncTask crash

I'm extending AsyncTask and in my doInBackground() I get a button view using findViewById and when I call button.performClick() my app crashes.

Any idea why?

this is the logcat:

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

Upvotes: 0

Views: 1038

Answers (1)

Sam
Sam

Reputation: 86948

I'm extending AsyncTask and in my doInBackground() I get a button view using findViewById and when I call button.performClick() my app crashes.

In AsyncTasks you can only work with UI elements, like Buttons, in onProgressUpdate(), onPostExecute() or other methods that have access to the UI thread. Simply move the code that calls on button into an acceptable method.

Upvotes: 5

Related Questions