本文介绍了Android的 - ViewRootImpl $ CalledFromWrongThreadException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是用这个,来显示图像从互联网但它抛出一个错误如下:
04-12 13:45:05.337:E / AndroidRuntime(27897):android.view.ViewRootImpl $ CalledFromWrongThreadException:所致。只有创建视图层次可以触摸其观点原来的线程

 公共类订单扩展活动{
        @覆盖
        公共无效的onCreate(包savedInstanceState){
            super.onCreate(savedInstanceState);
            新DownloadFilesTask()执行()。
        }
        私有类DownloadFilesTask扩展的AsyncTask<虚空,虚空,虚空> {
            保护无效onPostExecute(无效的结果){
            }
             @覆盖
             保护无效doInBackground(虚空...... PARAMS){
                 的setContentView(R.layout.order);
                    ImageView的ImageView的=(ImageView的)findViewById(R.id.imgView);
                    imageView.setImageDrawable(createDrawableFromURL(http://savagelook.com/misc/sl_drop2.png));
                    返回null;
             }
        }
        私人绘制对象createDrawableFromURL(字符串urlString){
            绘制的图像= NULL;
        尝试 {
            网址URL =新的URL(urlString);
            InputStream的是=(的InputStream)url.getContent();
            图像= Drawable.createFromStream(是,SRC);
        }赶上(MalformedURLException异常E){
            图像= NULL;
        }赶上(IOException异常E){
            图像= NULL;
        }
        返回形象;
        }

}
 

解决方案

将这个在的onCreate()

 的ImageView ImageView的;

    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.order);
        ImageView的=(ImageView的)findViewById(R.id.imgView);
        新DownloadFilesTask()执行()。
    }
 

和您的的AsyncTask 类应该是这样的,

 私有类DownloadFilesTask扩展的AsyncTask<虚空,虚空,虚空> {
             绘制对象绘制;

             @覆盖
             保护无效doInBackground(虚空...... PARAMS){
             绘制= createDrawableFromURL(
                                   http://savagelook.com/misc/sl_drop2.png);
              返回null;
             }
             保护无效onPostExecute(无效的结果){
                    imageView.setImageDrawable(绘制);
            }
        }
 

I was using this, to DISPLAY IMAGES FROM THE INTERNET but it throws an error as below:
04-12 13:45:05.337: E/AndroidRuntime(27897): Caused by: android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.

public class Order extends Activity {
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            new DownloadFilesTask().execute();       
        }    
        private class DownloadFilesTask extends AsyncTask<Void, Void, Void> {
            protected void onPostExecute(Void result) {
            }
             @Override
             protected Void doInBackground(Void... params) {
                 setContentView(R.layout.order);
                    ImageView imageView = (ImageView)findViewById(R.id.imgView);  
                    imageView.setImageDrawable(createDrawableFromURL("http://savagelook.com/misc/sl_drop2.png"));
                    return null;
             }          
        }     
        private Drawable createDrawableFromURL(String urlString) {
            Drawable image = null;
        try {
            URL url = new URL(urlString);
            InputStream is = (InputStream)url.getContent();
            image = Drawable.createFromStream(is, "src");
        } catch (MalformedURLException e) {
            image = null;
        } catch (IOException e) {
            image = null;
        } 
        return image;
        }

}
解决方案

Put this in onCreate()

ImageView imageView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.order);
        imageView = (ImageView)findViewById(R.id.imgView);
        new DownloadFilesTask().execute();       
    } 

And your AsyncTask class should be like this,

        private class DownloadFilesTask extends AsyncTask<Void, Void, Void> {
             Drawable drawable;

             @Override
             protected Void doInBackground(Void... params) {
             drawable = createDrawableFromURL(
                                   "http://savagelook.com/misc/sl_drop2.png");
              return null;
             }
             protected void onPostExecute(Void result) {
                    imageView.setImageDrawable(drawable);
            }          
        } 

这篇关于Android的 - ViewRootImpl $ CalledFromWrongThreadException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-12 12:59