我有以下代码在另一个线程中运行函数:
Button buttonb = (Button) this.findViewById(R.id.buttonb);
buttonb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
…
progressBar.setVisibility(View.VISIBLE);
Thread thread = new Thread() {
@Override
public void run() {
matrixOperation(sourcePhoto);
}
};
thread.start();
progressBar.setVisibility(View.INVISIBLE);
…
}
});但是在运行过程中,我得到了这个错误:
Can't create handler inside thread that has not called Looper.prepare()我搜索并发现,造成此错误的一个原因是“您不能从后台线程执行AsyncTask。请参阅“线程规则”部分,但这不是我从主要活动中调用的后台线程。
请告诉我怎么解决这个问题。
发布于 2013-11-13 22:10:29
Handler类使用Looper来执行它的调度,而刚刚创建的线程没有关联的活套,因此出现了错误。
由于您没有提供处理程序创建代码,所以我假设您希望调用主线程上的代码。在本例中,按照以下方式创建Handler:
Handler handler = new Handler(Looper.getMainLooper());计划在该Handler上运行的任何内容都将在主线程上运行的主Looper上执行。
https://stackoverflow.com/questions/19965358
复制相似问题