0

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

Asha S
  • 51
  • 1
  • 7

2 Answers2

0

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();
    }
Chintan Raghwani
  • 3,370
  • 4
  • 22
  • 33
0

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.

Kirill Gamazkov
  • 3,277
  • 1
  • 18
  • 22