IllegalStateException in AsyncTask

六眼飞鱼酱① 提交于 2020-01-06 19:35:46

问题


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

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

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

How to solve this?

Thanks in Advance


回答1:


Try following:

@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();
    }



回答2:


First time you add your eachLayout to some view it's OK. But when you do the same thing second time, eachLayout is already has parent. So attempt to add it to any view will fail. Try Chintan Raghwani's suggestion.



来源:https://stackoverflow.com/questions/12261285/illegalstateexception-in-asynctask

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!