[英]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.