gpt4 book ai didi

android - CalledFromWrongThreadException

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:04:33 33 4
gpt4 key购买 nike

 public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final ThraedDemo objDemo = new ThraedDemo();
Thread objThread = new Thread() {

@Override
public void run() {

objDemo.firstMethod();
}
};
objThread.start();
}


class ThraedDemo {

private void firstMethod() {
Thread objThread = new Thread() {

@Override
public void run() {
try {
((ImageView)findViewById(R.id.ImageViewnumber)).setImageResource(nums[n]);

Thread.sleep(10000);
Log.v("Thread","1111111111111111sleep");
} catch (InterruptedException ex) {
System.out.println("interuped exception" + ex.getMessage());
}
secondMethod();
}

private void secondMethod() {
Thread objThread = new Thread() {

@Override
public void run() {
try {
((ImageView)findViewById(R.id.ImageViewResult)).setImageResource(nums[n+1]);

n++;

Thread.sleep(10000);
Log.v("Thread","22222222222 sleep");
} catch (InterruptedException ex) {
System.out.println("interuped exception" + ex.getMessage());
}
firstMethod();
}
};
objThread.start();
}

};
objThread.start();
}

}

我使用上面的代码,但它没有运行。它得到 CalledFromWrongThreadException 上面代码中的问题是什么。请给我一些建议。提前致谢

最佳答案

我认为您不能从 UI 线程以外的其他线程进行 View 修改,因此要么创建 handlers在 oncreate 中发布你的线程,或者使用 AsyncTask , 或 runOnUIThread将部分代码直接发送到 UI 线程的方法。

关于android - CalledFromWrongThreadException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3413544/

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