Android - ViewRootImpl $ CalledFromWrongThreadException

时间:2012-04-12 06:14:41

标签: java android imageview

我正在使用this来显示来自互联网的图片,但它会抛出如下错误:
04-12 13:45:05.337:E / AndroidRuntime(27897):引起:android.view.ViewRootImpl $ CalledFromWrongThreadException:只有创建视图层次结构的原始线程才能触及其视图。

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

}

4 个答案:

答案 0 :(得分:26)

我在尝试使用JNI从c ++更改UI视图时遇到了同样的问题。解决方案是使用

runOnUiThread(new Runnable() {
    public void run(){   
    }
});

runOnUiThread是一个Activity方法,因此我必须使我的活动实例公共静态能够调用我后来从JNI调用的公共静态方法。

希望这有助于其他人:)

PS:从这里我学习如何使用JNI http://www.cocos2d-x.org/projects/cocos2d-x/wiki/How_to_use_jni为我以前使用cocos2dx制作的安卓游戏

答案 1 :(得分:19)

将其放入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();       
    } 

你的AsyncTask课应该是这样的,

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

答案 2 :(得分:2)

我认为此行导致错误..

  imageView.setImageDrawable(createDrawableFromURL("http://savagelook.com/misc/sl_drop2.png"));

错误解释了为什么会这样..

     Only the original thread that created a view hierarchy can touch its views.

导致此错误是因为您尝试从其他线程更改mainthread上的用户界面..在您的情况下 doInBackground ...

答案 3 :(得分:0)

如果您想在片段中完成此工作,则应从片段中的活动调用UI线程。

getActivity().runOnUiThread(new Runnable() {
  public void run(){

    ... //your job 

  }
});

@canerkaseler