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.
Why not to move View creation outside the task or may be in
onProgressUpdatebecause it is running on the UI-thread, anddoInBackgroundis not.