Styler2go
Styler2go

Reputation: 603

Remove View in ViewGroup

I got some problems using ViewGroup and a automatically generated ViewPager. I want to add a View and remove a view but everytime I am trying it I get the error:

    01-20 14:05:40.028: E/AndroidRuntime(3615): FATAL EXCEPTION: AsyncTask #1
    01-20 14:05:40.028: E/AndroidRuntime(3615): java.lang.RuntimeException: An error occured while executing doInBackground()
    01-20 14:05:40.028: E/AndroidRuntime(3615):     at android.os.AsyncTask$3.done(AsyncTask.java:299)
    01-20 14:05:40.028: E/AndroidRuntime(3615):     at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
    01-20 14:05:40.028: E/AndroidRuntime(3615):     at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
    01-20 14:05:40.028: E/AndroidRuntime(3615):     at java.util.concurrent.FutureTask.run(FutureTask.java:239)
    01-20 14:05:40.028: E/AndroidRuntime(3615):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
    01-20 14:05:40.028: E/AndroidRuntime(3615):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
    01-20 14:05:40.028: E/AndroidRuntime(3615):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
    01-20 14:05:40.028: E/AndroidRuntime(3615):     at java.lang.Thread.run(Thread.java:856)
    01-20 14:05:40.028: E/AndroidRuntime(3615): Caused by: android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
    01-20 14:05:40.028: E/AndroidRuntime(3615):     at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:4746)
    01-20 14:05:40.028: E/AndroidRuntime(3615):     at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:823)
    01-20 14:05:40.028: E/AndroidRuntime(3615):     at android.view.View.requestLayout(View.java:15468)
    01-20 14:05:40.028: E/AndroidRuntime(3615):     at android.view.View.requestLayout(View.java:15468)
    01-20 14:05:40.028: E/AndroidRuntime(3615):     at android.view.View.requestLayout(View.java:15468)
    01-20 14:05:40.028: E/AndroidRuntime(3615):     at android.view.View.requestLayout(View.java:15468)
    01-20 14:05:40.028: E/AndroidRuntime(3615):     at android.view.ViewGroup.removeView(ViewGroup.java:3524)
    01-20 14:05:40.028: E/AndroidRuntime(3615):     at de.tecfriends.vbtsplash2013.MainActivity$DummySectionFragment.Teilnehmer(MainActivity.java:219)
    01-20 14:05:40.028: E/AndroidRuntime(3615):     at de.tecfriends.vbtsplash2013.MainActivity$DummySectionFragment$1.onTaskCompleted(MainActivity.java:148)
    01-20 14:05:40.028: E/AndroidRuntime(3615):     at de.tecfriends.vbtsplash2013.BackgroundDownload.doInBackground(BackgroundDownload.java:38)
    01-20 14:05:40.028: E/AndroidRuntime(3615):     at de.tecfriends.vbtsplash2013.BackgroundDownload.doInBackground(BackgroundDownload.java:1)
    01-20 14:05:40.028: E/AndroidRuntime(3615):     at android.os.AsyncTask$2.call(AsyncTask.java:287)
    01-20 14:05:40.028: E/AndroidRuntime(3615):     at java.util.concurrent.FutureTask.run(FutureTask.java:234)
    01-20 14:05:40.028: E/AndroidRuntime(3615):     ... 4 more

In the Class onCreateView i set a public ViewGroup named vGroup. At the end of all data processing i try to vGroup.removeView(vGroup.findViewById(1)); and vGroup.addView(modeList); but i get the error above.

How can I reach the original Thread where I can add and remove Views to this ViewGroup?

Upvotes: 1

Views: 1171

Answers (3)

user2494388
user2494388

Reputation: 1

next to use vGroup.removeView() you need to call vGroup.refreshDrawableState()

Upvotes: 0

Swayam
Swayam

Reputation: 16354

ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views. means that you are trying to access UI elements from the background thread, which is the non-UI thread.

vGroup.removeView() should only be called from onPreExecute() or onPostExecute(), as these two functions run on the UI thread.

private class MyAsyncTask extends AsyncTask<String, Void, String> {

      @Override
      protected String doInBackground(String... params) {
            .... // runs on background / non-UI thread
      }      

      @Override
      protected void onPostExecute(String result) {   
            ... // runs on UI thread            
      }

      @Override
      protected void onPreExecute() {
            ... // runs on UI thread      
      }

      @Override
      protected void onProgressUpdate(Void... values) {
      }
}

Documentation of AsyncTask.

Upvotes: 2

Ovidiu Latcu
Ovidiu Latcu

Reputation: 72311

You are calling vGroup.removeView() from a background thread. To alter the UI you should/must always use the UI Thread.

EDIT: create a Handler on the UIThread and use mHandler.post(new Runnable());

Upvotes: 1

Related Questions