我收到了这段代码:
public void make_square(View v) {
TextView txt_wait = (TextView) findViewById(R.id.square_txt);
txt_wait.setText("wait...");
try{
//rest of the code (scanning a bitmap for pixels)
“make_square
”是一个onClick事件(在XML文件中)。
当我点击我想要更改文本(txt_wait
)
然后在更改之后,他必须执行剩下的代码。
但不知何故,它等待代码的其余部分,然后当代码完成时,他改变了txt_wait
我也尝试过这样的事情(既没有效果):
public void make_square(View v) {
TextView txt_wait = (TextView) findViewById(R.id.square_txt);
txt_wait.setText("wait...");
make_square2(v);
}
public void make_square2(View v){
try{
//rest of the code (scanning a bitmap for pixels)
}
}
但现在,它在更改文本之前首先make_square2
。
为什么或如何解决这个问题?它首先改变txt_wait
,然后改变它,然后改变代码的其余部分?
谢谢, Bigflow
EDIT1:
尝试了这个:
public void make_square(final View v) {
TextView txt_wait = (TextView) findViewById(R.id.square_txt);
txt_wait.setText("wait...");
new Thread() {
public void run() {
make_square2(v);
}
}.start();
}
public void make_square2(View v){
try{
//rest of the code
}
}
它给出了我的错误:
E/AndroidRuntime(17487): android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
我也试过这个:
public void make_square(View v) {
TextView txt_wait = (TextView) findViewById(R.id.square_txt);
txt_wait.setText("wait...");
make_square2(v);
}
public void make_square2(View v)
new Thread() {
public void run() {
//rest of the code
}
}.start();
这也给出了完全相同的错误。
我对线程使用不熟悉,也许我做的事非常糟糕。 希望你能再次帮助我。
答案 0 :(得分:0)
我认为你可以为make_square2方法中的代码创建一个单独的线程..然后文本将被更改...其余的代码将在除UI线程之外的线程中运行..这似乎阻止了UI线程..
答案 1 :(得分:0)
我使用AsyncTask来完成这项工作,不知何故,线程方法对我不起作用。
感谢AdilSoomro和JayeshPate。