gpt4 book ai didi

java - Android - ViewRootImpl$CalledFromWrongThreadException

转载 作者:IT老高 更新时间:2023-10-28 20:56:55 27 4
gpt4 key购买 nike

我使用的是 this , 显示来自互联网的图像,但它会引发如下错误:
04-12 13:45:05.337: E/AndroidRuntime(27897): Caused by: android.view.ViewRootImpl$CalledFromWrongThreadException: 只有创建 View 层次结构的原始线程才能触摸其 View 。

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

}

最佳答案

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

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

runOnUiThread 是一个 Activity 方法,因此我必须将我的 Activity 实例设为公共(public)静态,以便能够调用稍后从 JNI 调用的我的公共(public)静态方法。

希望这对其他人有所帮助:)

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

关于java - Android - ViewRootImpl$CalledFromWrongThreadException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10118301/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com