简体   繁体   English

Android - ViewRootImpl$CalledFromWrongThreadException

[英]Android - ViewRootImpl$CalledFromWrongThreadException

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

}

I got the same problem trying to change UI view from c++ using JNI.我在尝试使用 JNI 从 C++ 更改 UI 视图时遇到了同样的问题。 The solution was use解决方案是使用

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

runOnUiThread is an Activity method so I have to make my activity instance public static to be able to call on my public static method who later call from JNI. runOnUiThread 是一个 Activity 方法,所以我必须将我的活动实例设为 public static 以便能够调用我后来从 JNI 调用的公共静态方法。

Hope this help others :)希望这对其他人有帮助:)

PS: from here I learn how to use JNIhttp://www.cocos2d-x.org/projects/cocos2d-x/wiki/How_to_use_jni for my android game previously made with cocos2dx PS:从这里我学习如何使用 JNIhttp://www.cocos2d-x.org/projects/cocos2d-x/wiki/How_to_use_jni为我以前用 cocos2dx 制作的 android 游戏

Put this in onCreate()把它放在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,而你的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);
            }          
        } 

I think this line is causing the error..我认为这条线导致了错误..

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

and the error explains why it is so..错误解释了为什么会这样..

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

this error is caused because you are trying to change the User Interface on mainthread from some other thread.. here doInBackground in your case...这个错误是因为你试图从其他线程更改主线程上的用户界面..在你的情况下这里doInBackground ......

If you would like to do this job in fragment, you should call UI thread from activity in fragment.如果你想在片段中完成这项工作,你应该从片段中的活动调用 UI 线程。

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

    ... //your job 

  }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 android.view.viewrootimpl $ fromwrongthreadexception android java - android.view.viewrootimpl$calledfromwrongthreadexception android java Android编码:ViewRootImpl $ CalledFromWrongThreadException。 [菜鸟] - Android coding: ViewRootImpl$CalledFromWrongThreadException. [Noob] Android动态文本更新-ViewRootImpl $ CalledFromWrongThreadException - Dynamic Text Update for Android - ViewRootImpl$CalledFromWrongThreadException 计时器导致ViewRootImpl $ CalledFromWrongThreadException吗? - Timer causing ViewRootImpl$CalledFromWrongThreadException? android.view.ViewRootImpl $ CalledFromWrongThreadException:只有创建视图层次结构的原始线程才能触摸其视图 - android.view.ViewRootImpl$CalledFromWrongThreadException:Only the original thread that created a view hierarchy can touch its views 为什么我收到异常ViewRootImpl $ CalledFromWrongThreadException? - Why i'm getting exception ViewRootImpl$CalledFromWrongThreadException? 当我按下按钮时,出现错误ViewRootImpl $ CalledFromWrongThreadException - When my button is pressed, I get the error ViewRootImpl$CalledFromWrongThreadException ViewRootImpl不在Android SDK中 - ViewRootImpl not in Android SDK CalledFromWrongThreadException在Android上执行JUnit测试 - CalledFromWrongThreadException exercising JUnit tests on Android Android Thread.start()CalledFromWrongThreadException - Android Thread.start() CalledFromWrongThreadException
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM