android中的痛苦线程

android中的痛苦线程,android,Android,我的预期结果是在第一次启动时,将显示一个进度对话框,等待后台线程加载内容。工作线程完成作业后,对话框将关闭。我做了搜索,得到了这个解决方案 这是我的完整代码: private ManagerApplication app; private ImageAdapter adapter; private GridView gridview; @Override public void onCreate(Bundle savedInstanceState) {

我的预期结果是在第一次启动时,将显示一个进度对话框,等待后台线程加载内容。工作线程完成作业后,对话框将关闭。我做了搜索,得到了这个解决方案

这是我的完整代码:

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

    }
私人管理应用程序;
专用图像适配器;
私有GridView GridView;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setupView();
}
私有void setupView(){
gridview=(gridview)findViewById(R.id.gridview);
app=(ManagerApplication)getApplication();
ProgressDialog进度=新建ProgressDialog(此);
progress.setMessage(“加载…”);
新建LoadImageTask(进度,this).execute();
}
私有类LoadImageTask扩展了AsyncTask{
私人进展对话进展;
私人语境;
公共LoadImageTask(进度对话框进度,上下文){
这个。进步=进步;
this.context=上下文;
}
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
progress.show();
}
@凌驾
受保护的Void doInBackground(Void…参数){
adapter=newimageadapter(app.getShoes(),context);
setAdapter(适配器);
返回null;
}
@凌驾
受保护的void onPostExecute(void结果){
super.onPostExecute(结果);
进步。解散();
}
}

但是,我的应用程序崩溃的原因是“只有创建视图层次结构的原始线程才能接触其视图”。我想在主UI线程中有一些东西被阻塞了,但它仍然非常不清楚。有人能告诉我这个问题吗?谢谢

有趣的是,这里有一篇题为“你应该阅读”的文章。您不应该试图在
doInBackground
中操纵视图。在您的情况下,您可以调用
app.getShoes()
,但不要在那里进行适配器设置

doinBackground是非UI线程,因此永远不要更新此线程中的任何UI(视图) 方法

并使用
OnPostExecute或OnProgressUpdate更新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;
        }
从setupview调用asynctask

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

        new LoadImageTask(progress, this).execute();       
  }
并在
OnPreExecute方法中创建ProgressDilog

ProgressDialog progress;

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

     progress = new ProgressDialog(this);
     progress.setMessage("Loading...");
     progress.show();
    }
onPostExecute中,为“您不应该试图在
doInBackground
中操纵视图”而取消它