程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> progressdialog-android.view.ViewRoot$CalledFromWrongThreadException 異常

progressdialog-android.view.ViewRoot$CalledFromWrongThreadException 異常

編輯:編程綜合問答
android.view.ViewRoot$CalledFromWrongThreadException 異常

我想從服務器中下載圖片後並顯示出來,當進行下載時,會顯示一個進度對話框。使用一個 asynctask 類。運用下面的代碼:

    private void startDownload() {

    new DownloadFileAsync().execute(imageUrl);
    image.setImageBitmap(bitmap);
}
 @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
            case DIALOG_DOWNLOAD_PROGRESS:
                dialog = new ProgressDialog(this);
                dialog.setTitle("Loading");
                dialog.setMessage("Please wait...");
                dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
                dialog.setCancelable(false);
                dialog.show();
                return dialog;
            default:
                return null;
        }
    }


 class DownloadFileAsync extends AsyncTask<String, String, String> {
     int count;
     URL myFileUrl;

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            showDialog(DIALOG_DOWNLOAD_PROGRESS);
        }

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


            try {
                myFileUrl = new URL(imageUrl);
                HttpURLConnection conn = (HttpURLConnection) myFileUrl
                        .openConnection();
                int lenghtOfFile = conn.getContentLength();
                //conn.setDoInput(true);
                conn.setConnectTimeout(10000);
                conn.setReadTimeout(10000);
                conn.connect();
                InputStream is = conn.getInputStream();
                bmImg = BitmapFactory.decodeStream(is);
                bitmap = BitmapFactory.decodeStream((InputStream) new URL(imageUrl)
                        .getContent());
                bitmap = Bitmap.createScaledBitmap(bitmap, 70, 70, true);

                  byte data[] = new byte[1024];
                  System.out.println("mmmmmmmmmmmm");

                    long total = 0;
                    System.out.println("nnnnnnnnnn");
                    while ((count = ((InputStream) new URL(imageUrl)
                    .getContent()).read(data)) != -1) {
                        total += count;
                        publishProgress(""+(int)((total*100)/lenghtOfFile));
                        for(int l=0;l<4;l++){
                        if(listObject.get(l).getImage()!="")
                       image.setImageBitmap(bitmap);
                    }}

            }
            catch(Exception e){
                System.out.println(e);}

        return null;
        }

        protected void onProgressUpdate(String... progress) {
            dialog.setProgress(Integer.parseInt(progress[0]));
        }

        @Override
        protected void onPostExecute(String unused) {
            image.setImageBitmap(bitmap);
            dismissDialog(DIALOG_DOWNLOAD_PROGRESS);

        }
        }

卻給出下面的異常:

android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views. 

我找不出錯誤所在,請大家幫忙改正。

最佳回答:


繼承自AsyncTask,注意AsyncTask最後的最後一個類型為Bitmap,然後 protected Bitmap doInBackground(String... aurl)函數返回獲得的圖片,
最後在

@Override protected void onPostExecute(Bitmap image) {
            image.setImageBitmap(image);
            dismissDialog(DIALOG_DOWNLOAD_PROGRESS);

        }

,
還有image.setImageBitmap()不能放到doInBackground()中,因為doInBackground()不是運行在主線程。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved