1

I am using the AsyncTask and I am getting the following error in the onProgressUpdate method:

java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first. at android.view.ViewGroup.addViewInner(ViewGroup.java:1970) at android.view.ViewGroup.addView(ViewGroup.java:1865) at android.widget.TableLayout.addView(TableLayout.java:418)

I know what this error means and how to resolve it. What I do not understand is why I am getting this error ONLY in debug mode. Adding a Thread.Sleep(1000) to the doInBackground method practically negates the issue, but I cannot add that for obvious performance reasons. Here is the gist of what is happening in my AsyncTask:

 protected Void doInBackground(Void... params) {

    row1 = TableVisuals.createRow(context, flag);
    tv1 = TableVisuals.getTextView(context, Info[0]);
    row2 = TableVisuals.createRow(context, flag);
    tv2 = TableVisuals.getTextView(context, Info[1]);
    publishProgress(i,0);

}

protected void onProgressUpdate(Integer... value) {

table.addView(row1);
row1.addView(tv1, 0, span);
table.addView(row2);
row2.addView(tv2, 0, span);

dialog.setProgress(value[0]);

}

Keep in mind, the IllegalStateException does not happen in any one part of the above code. It is inconsistent in places, but almost always occurs. I wish there was a way for the background thread to wait for the UI thread other than Thread.Sleep() because it appears that it is adding views too fast in some cases.

Gaff
  • 5,507
  • 3
  • 38
  • 49

1 Answers1

0

Why not to move View creation outside the task or may be in onProgressUpdate because it is running on the UI-thread, and doInBackground is not.

Olegas
  • 10,349
  • 8
  • 51
  • 72
  • Well, the problem is that I have around 30 View Creations and some of them have a little work attached to them, such as an image view that grab's a contacts synced facebook photo from the database. It is probably poor coding on my part, but putting the creation of those 30 views into the UI thread actually makes results inconsistent and spotty. – Gaff May 29 '11 at 20:29