使线程在多个图像视图中加载多个图像不起作用?
Making thread to load multiple images in multiple imageviews not working?
好吧,我想用一种方法在五个图像视图中加载五个图像 activity。图片是 jpeg 格式的短文件(最大 500kb)所以我在 Oncreate
:
上试过这段代码
new Thread(new Runnable() {
public void run(){
ins1 = (ImageView) findViewById(R.id.ins1);
//and four more of this imagesviews
ins5 = (ImageView) findViewById(R.id.ins5);
d1 = getResources().getDrawable(R.drawable.ins1);
//four more
ins1.setImageDrawable(d1);
// and four more
}
我收到了这个日志:
03-07 09:40:52.395: E/AndroidRuntime(3662): FATAL EXCEPTION: Thread-2727
03-07 09:40:52.395: E/AndroidRuntime(3662): Process: com.karlol.modernoapp, PID: 3662
03-07 09:40:52.395: E/AndroidRuntime(3662): android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
我从未使用过 AsyncTask,这可能是一个选项,我需要帮助
只有创建视图层次结构的原始线程才能触及它的视图。
发生此错误是因为您试图在工作线程中而不是在 UI 线程中将可绘制对象绑定到图像视图。
下面是asyntask的例子
AsyncTask Android example
好吧,我想用一种方法在五个图像视图中加载五个图像 activity。图片是 jpeg 格式的短文件(最大 500kb)所以我在 Oncreate
:
new Thread(new Runnable() {
public void run(){
ins1 = (ImageView) findViewById(R.id.ins1);
//and four more of this imagesviews
ins5 = (ImageView) findViewById(R.id.ins5);
d1 = getResources().getDrawable(R.drawable.ins1);
//four more
ins1.setImageDrawable(d1);
// and four more
}
我收到了这个日志:
03-07 09:40:52.395: E/AndroidRuntime(3662): FATAL EXCEPTION: Thread-2727
03-07 09:40:52.395: E/AndroidRuntime(3662): Process: com.karlol.modernoapp, PID: 3662
03-07 09:40:52.395: E/AndroidRuntime(3662): android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
我从未使用过 AsyncTask,这可能是一个选项,我需要帮助
只有创建视图层次结构的原始线程才能触及它的视图。
发生此错误是因为您试图在工作线程中而不是在 UI 线程中将可绘制对象绑定到图像视图。
下面是asyntask的例子
AsyncTask Android example