0

I have an Asynctask which is inside of an activity task. When I try to create a new view inside of the Asynctask it gives me an error and says its undefined. Here is what my code basically is.

    public class DashboardActivity extends Activity {

                class loadComments extends AsyncTask<String, String, String> {
            @Override
            protected void onPreExecute() {
                super.onPreExecute();
            }

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

            }

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

            LinearLayout commentBox = new LinearLayout(this);





                return null;
            }

            @Override
            protected void onPostExecute(String result) {
                super.onPostExecute(result);

            }
        }// end asynctask
    }//end activity

The error occurs when I try to make the linearLayout or any type of view. I have tried taking away the (this) statement and placing the code everywhere.

4

3 回答 3

1

One should not perform any UI operation in AsyncTask's doInbackground which off the UI thread, doInBackground invokes background thread.

Instead you can perform the UI operation in onProgressUpdate , onPostExecute and onPreExecute

For more information refer http://developer.android.com/reference/android/os/AsyncTask.html

于 2013-07-05T05:22:37.360 回答
0

You can not do that.

LinearLayout commentBox = new LinearLayout(this);

Do it in the UI Thread, meaning move it outside doInBackground().

于 2013-07-05T05:17:51.270 回答
0

doInbackground runs on the background thread. So you cannot access/update ui from the background thread. You can update ui on the ui thread.

"Only the original thread that created a view hierarchy can touch its views.".

You will see the above message if you update ui from the background thread.

onPreExecute and onPostExecute are invoked on the ui thread. You can return the result in doInBackground and update ui in onPostExecute. The result of doInbackground computation is a parameter to onPostExecute.

Since you had this

    LinearLayout commentBox = new LinearLayout(this);

this does not refer to the activity context in your case. Even if you use activity context and initiliaze Linear layout you need to add the same to your root layout.

http://developer.android.com/reference/android/os/AsyncTask.html

      public class DashboardActivity extends Activity {
      LinearLayout layout;
      @Override
      public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      layout = new LinearLayout(this);
      setContentView(layout); 
      new loadComments().execute(params);
      }
        class loadComments extends AsyncTask<String, String, String> {
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

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

        }

        @Override
        protected String doInBackground(String... params) {
            return null;
        }

        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);

        }
      }
于 2013-07-05T05:25:28.377 回答