【问题标题】:IllegalStateException in AsyncTaskAsyncTask 中的 IllegalStateException
【发布时间】:2012-09-04 10:12:39
【问题描述】:

我有一个 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); .(这个孩子已经有了父母)。

我尝试过 linearLayout.removeAllViews() 等...但它不起作用。

如何解决?

提前致谢

【问题讨论】:

  • 为什么要在onProgressUpdate中添加视图?
  • 你之前在哪个布局中添加了eachLayout?
  • 我想膨胀它的内容。onProgressUpdate() 是 ui 线程。
  • linearlayout,即父级。

标签: android exception android-asynctask


【解决方案1】:

尝试以下操作:

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

【讨论】:

  • Sorry.It throws NullPointerException
【解决方案2】:

第一次将 eachLayout 添加到某个视图时,没关系。但是当你第二次做同样的事情时, eachLayout 已经有了父级。因此,尝试将其添加到任何视图都会失败。试试 Chintan Raghwani 的建议。

【讨论】:

    猜你喜欢
    • 2018-03-30
    • 1970-01-01
    • 2014-08-04
    • 1970-01-01
    • 2014-03-28
    • 2020-06-02
    • 2013-06-25
    • 2011-06-26
    • 2014-05-13
    相关资源
    最近更新 更多