0

私はこのコードを手に入れました:

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

編集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
   }
}

そしてそれは私のこのエラーを与えます:

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();

また、これによりまったく同じエラーが発生します。

私はスレッドの使用に慣れていません。多分私は非常に悪いことをします。もう一度助けていただければ幸いです。

4

2 に答える 2

0

make_square2メソッドでコード用に別のスレッドを作成できると思います。その後、テキストが変更されます...残りのコードはUIスレッド以外のスレッドで実行されます。これはUIスレッドをブロックしているようです。 。

于 2012-05-10T10:14:34.577 に答える
0

私はこれを行うためにAsyncTaskを使用しましたが、どういうわけかスレッドメソッドが機能していませんでした。

私はこのチュートリアルを使用しました: http ://www.android10.org/index.php/forums/43-view-layout-a-resource/908-tutorial-progressbar-running-in-asynctask

AdilSoomroとJayeshPateに感謝します。

于 2012-05-11T09:32:59.053 に答える