Painful Android thread

My expected result in the first run will display a progress dialog to wait for the background content stream to load. The dialog is rejected after the workflow has completed the task. I did a search and got this solution. How to display a progress dialog before starting work in Android?

This is my complete code:

    private ManagerApplication app;
    private ImageAdapter adapter;
    private GridView gridview;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        setupView();      
    }

    private void setupView() {
        gridview = (GridView) findViewById(R.id.gridview);
        app = (ManagerApplication) getApplication();
        ProgressDialog progress = new ProgressDialog(this);
        progress.setMessage("Loading...");
        new LoadImageTask(progress, this).execute();       
    }

    private class LoadImageTask extends AsyncTask<Void, Void, Void> {
        private ProgressDialog progress;
        private Context context;

        public LoadImageTask(ProgressDialog progress, Context context) {
            this.progress = progress;
            this.context = context;
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            progress.show();
        }

        @Override
        protected Void doInBackground(Void... params) {
            adapter = new ImageAdapter(app.getShoes(), context);
            gridview.setAdapter(adapter); 
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
            progress.dismiss();
        }

    }

However, my application crashes because "Only the original thread that created the hierarchy of views can touch its views." I suppose something blocked the main UI thread, but its still very obscure. So can anyone point me to a problem? Thanks

+3
source share
2 answers

doinBackground - UI Thread, - (View) ...

OnPostExecute or OnProgressUpdate for update UI

 @Override
        protected Void doInBackground(Void... params) {
           //here just background task ,
     //its non UI Thread so dont set ant view here set it in OnPostExecute,...
            return null;
        }

asynctask setupview

private void setupView() {
        gridview = (GridView) findViewById(R.id.gridview);
        app = (ManagerApplication) getApplication();

        new LoadImageTask(progress, this).execute();       
  }

ProgressDilog OnPreExecute method

ProgressDialog progress;

@Override
    protected void onPreExecute() {
        super.onPreExecute();

     progress = new ProgressDialog(this);
     progress.setMessage("Loading...");
     progress.show();
    }

onPostExecute dismiss it

+2

, Painless Threading, . doInBackground. app.getShoes(), .

+5

All Articles