android.view.ViewRoot $ CalledFromWrongThreadException:只有创建视图层次可以触摸其观点原来的线程。 [英] android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.

查看:148
本文介绍了android.view.ViewRoot $ CalledFromWrongThreadException:只有创建视图层次可以触摸其观点原来的线程。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发一个Android应用程序中,我需要从服务器下载它们,当下载正在进行进度对话框正在显示后显示的图像。对于我的使用AsyncTask的类。 我使用的T他下面的源$ C ​​$ C吧。

I am developing an android app in which i need to display images after downloading them from server and when the downloading is proceeding a progress dialog is being show. For that i an using an asynctask class. I am using t he following source code for it.

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

        }
        }

但它给了以下异常:

But it giving the following exception.:

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

我无法找出问题。谁能帮我过这一点。 谢谢

i am unable to figure out the problem. Can anyone help me over this. Thanks

推荐答案

这是你的问题:

for(int l=0;l<4;l++){
    if(listObject.get(l).getImage()!="")
        image.setImageBitmap(bitmap);
}

简短的回答:就像例外说,你想操纵错误的线程的视图。做到在正确的线程(UI线程)。

Short answer: just like the exception says, you're trying to manipulate a view in the wrong thread. Do it in the right thread (UI thread).

龙答:在一个AsyncTask的正确的地方做视图操作一般在preExecute onProgressUpdate onPostExecute doInBackground 通常不是一个好地方修改意见。您的可以的回调到UI线程在众多方法之一(例如,你可以使用<一个href="http://developer.android.com/reference/android/view/View.html#post%28java.lang.Runnable%29"><$c$c>post).但是,你贴不作一大堆意义,我在一般情况下,你还没有表现出足够的上下文来解释什么是code表示块,什么的ListObject 是,等。

Long answer: In an AsyncTask the right places to do view manipulation are generally onPreExecute, onProgressUpdate and onPostExecute. doInBackground is not usually a good place to modify views. You could call back to the UI thread in one of many ways (for example you could use post). However, that block of code that you posted doesn't make a whole lot of sense to me in general, and you haven't shown enough context to explain what it is, what listObject is, etc.

您有一些其他的问题了。你似乎在试图读取两次数据在连续两种不同的方式......还有,我想你的,而条件是要给你的问题​​,你'再重新创建URL对象,只要你的内容回来你要继续做下去。假设URL没有动态的内容,你就会有一个无限循环。

You have some other problems too. You seem to be trying to read the data in twice in a row in two different ways... Also, I imagine your while condition is going to give you problems as you're recreating the URL object and as long as you get content back you're going to keep doing it. Assuming that URL doesn't have dynamic content, you'll have an infinite loop.

这篇关于android.view.ViewRoot $ CalledFromWrongThreadException:只有创建视图层次可以触摸其观点原来的线程。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆