Java 强制首先执行代码的特定部分
我得到了这个密码:Java 强制首先执行代码的特定部分,java,android,textview,Java,Android,Textview,我得到了这个密码: 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是XML文件中的onClick事件。 当我点击我想更改文本时,请等待 然后在代码更改后,他必须执
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是XML文件中的onClick事件。
当我点击我想更改文本时,请等待
然后在代码更改后,他必须执行代码的其余部分。
但不知何故,它会等待代码的其余部分,然后当代码完成时,他会更改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)
}
}
但现在,它在改变文本之前,先做2平方。
为什么或如何修复此问题?它会先更改txt_wait,然后再更改txt_wait,然后再更改其余的代码吗
已经谢谢了,
大流量
编辑1:
我试过这个:
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
}
}
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();
这给了我一个错误:
E/AndroidRuntime(17487): android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
我也试过:
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
}
}
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();
这也给出了完全相同的错误
我不熟悉线程的使用,可能我做了一些非常糟糕的事情。
希望你能再次帮助我 我认为可以在make_square2方法中为代码创建一个单独的线程。。然后文本将被更改…剩下的代码将在UI线程以外的线程中运行..这似乎阻止了UI线程..我使用AsyncTask来完成此任务,不知何故线程方法对我不起作用 我使用了本教程:
多亏了AdilSoomro和JayeshPate。我试过了,但没有成功。请你再看一下好吗?在编辑完1OK之后。。。我认为在你的代码中,你试图改变UI中的某些东西。。。它不能从任何其他线程以外的mainb线程完成。。。所以在你的线程中,把改变用户界面的代码放在runOnUiThread方法中抱歉,我不知道你的确切意思,我是android编程的初学者,你能用另一种方式解释一下吗,或者其他解决方案?请参阅我的答案。关于这个。并将runonumethod放在线程的run方法中…而不是Connect.setBackgroundResourceR.drawable.contect_按钮;这一行放入更改用户界面的代码。。。好的,现在我做了和EDIT1一样的事情,但是用了RunNuithRead方法。它没有给出错误,但现在我遇到了与EDIT1之前相同的问题,因此它在完成其余代码后更改了文本。