java.lang.IllegalStateException将在调试模式视图时 [英] java.lang.IllegalStateException when adding a View in debug mode

查看:144
本文介绍了java.lang.IllegalStateException将在调试模式视图时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是AsyncTask的,我凑了onProgressUpdate方法如下错误:

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

java.lang.IllegalStateException:本
  指定的子已经有一个父。
  您必须在调用removeView()
  孩子的父母第一次。在
  android.view.ViewGroup.addViewInner(ViewGroup.java:1970)
  在
  android.view.ViewGroup.addView(ViewGroup.java:1865)
  在
  android.widget.TableLayout.addView(TableLayout.java:418)

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)

我知道这是什么错误意味着以及如何解决它。我不明白的是为什么我只在调试模式下收到此错误。添加了Thread.Sleep(1000)doInBackground方法实际上否定了这个问题,但我不能补充的是,明显的性能的原因。
这里的要点是什么在我的AsyncTask发生了什么:

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]);

}

请记住,在IllegalStateException异常没有在上面code中的任何一个部分发生。它是在场所不一致,但几乎总是发生。我希望有后台线程等待超过了Thread.Sleep()其他UI线程的方式,因为它看来,它在某些情况下,过快增加的观点。

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.

推荐答案

为什么不移动视图创建任务之外,也可以在 onProgressUpdate ,因为它是在运行UI线程,和 doInBackground 不是

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.

这篇关于java.lang.IllegalStateException将在调试模式视图时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆