IllegalStateException异常中的AsyncTask [英] IllegalStateException in AsyncTask

查看:186
本文介绍了IllegalStateException异常中的AsyncTask的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个AsyncTask的。

I have an AsyncTask.

protected class InitTask extends AsyncTask<Context, Integer, String> {

        View eachLayout;

        @Override
        protected String doInBackground(Context... params) {

            try {
                myfunction();
            } catch (Exception e) {

                e.printStackTrace();

            }

            return null;
        }

        @Override
        protected void onPostExecute(String result) {

            super.onPostExecute(result);


        }

        /*
         * (non-Javadoc)
         * 
         * @see android.os.AsyncTask#onPreExecute()
         */
        @Override
        protected void onPreExecute() {
            super.onPreExecute();

            linearLayout.invalidate();
        }

        @Override
        protected void onProgressUpdate(Integer... values) {
            super.onProgressUpdate(values);

            linearLayout.addView(eachLayout, params);
            linearLayout.invalidate();
        }

        /*
         * (non-Javadoc)
         * 
         * @see android.os.AsyncTask#onCancelled()
         */
        @Override
        protected void onCancelled() {
            super.onCancelled();
        }

        public void redrawLayout(View linearLayout) {
            try {

                eachLayout = linearLayout;
                publishProgress();

            } catch (Exception e) {

                e.printStackTrace();

            }
        }

    }


private void myFunction() {
        LayoutInflater layoutInflater = (LayoutInflater) this
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        params = new LinearLayout.LayoutParams(
                newLinearLayout.getLayoutParams().width,
                newLinearLayout.getLayoutParams().height);

        for (int i = 0; i < list.size(); i++) {
            final View eachLayout = layoutInflater.inflate(R.layout.sample, null);
            //..........................
            eachLayout.invalidate();
            initTask.redrawLayout(eachLayout);
        }

    }


 params = new LinearLayout.LayoutParams(
                linearLayout.getLayoutParams().width,
                linearLayout.getLayoutParams().height);

它显示的 IllegalStateException异常是在这条线
 linearLayout.addView(eachLayout,则params); (这个孩子已经有一个父)。

It shows IllegalStateException at this line linearLayout.addView(eachLayout, params); .(This child already has a parent).

我试过linearLayout.removeAllViews()等等...但它不工作。

I am tried linearLayout.removeAllViews() etc...But it does not work.

如何解决此问题?

由于提前

推荐答案

尝试以下操作:

@Override
    protected void onProgressUpdate(Integer... values) {
        super.onProgressUpdate(values);

       if( ((LinearLayout) eachLayout.getParent()) != null 
                &&  eachLayout != null) {
             ((LinearLayout) eachLayout.getParent()).removeView(eachLayout);
       }

        linearLayout.addView(eachLayout, params);
        linearLayout.invalidate();
    }

这篇关于IllegalStateException异常中的AsyncTask的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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