【问题标题】:Painful threading in androidandroid中的痛苦线程
【发布时间】:2012-05-11 19:11:42
【问题描述】:

我的预期结果是在第一次启动时会显示一个进度对话框来等待后台线程加载内容。工作线程完成工作后,对话框消失。我确实搜索并得到了这个解决方案How to display progress dialog before starting an activity in Android?

这是我的完整代码:

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

    }

但是,我的应用程序崩溃,原因是“只有创建视图层次结构的原始线程才能触及其视图”。我想有一些东西阻塞了主 UI 线程,但它仍然很不清楚。那么有人可以指出我的问题吗?谢谢

【问题讨论】:

    标签: android


    【解决方案1】:

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

    【讨论】:

    • +1 表示“您不应该尝试在 doInBackground 中操纵视图”
    【解决方案2】:

    doinBackground 是非 UI 线程,因此永远不要在此更新任何 UI(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;
            }
    

    从 setupview 调用 asynctask

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

    并在OnPreExecute method中创建ProgressDilog

    ProgressDialog progress;
    
    @Override
        protected void onPreExecute() {
            super.onPreExecute();
    
         progress = new ProgressDialog(this);
         progress.setMessage("Loading...");
         progress.show();
        }
    

    onPostExecute dismiss it

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-12-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-25
      • 2012-02-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多