0

アクティビティを作成した後、電話帳から読み取り、文字列操作を行い、ルールに基づいてメイン UI にさまざまなボタンを作成するスレッドを作成しました。

私は以下のコードを持っていますが、アプリケーションがどのように終了するかです。

誰でも私を助けてください。

public class Phone extends Activity {
    /** Called when the activity is first created. */
    ProgressDialog pbarDialog;
    LinearLayout ll;
    Button b;

    @Override    
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ScrollView sv = new ScrollView(this);
        ll = new LinearLayout(this);
        ll.setOrientation(LinearLayout.VERTICAL);
        sv.addView(ll);
        setContentView(sv);
        pbarDialog = new ProgressDialog(this);
        pbarDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        pbarDialog.setMessage("Reading from Phone Book");
        pbarDialog.setCancelable(false);
        pbarDialog.show();
        pbarDialog.incrementProgressBy(0);     



        new TheTask().execute();                       

    }


    private class TheTask extends AsyncTask<Void, Integer, Void>{

        @Override
        protected void onPreExecute() {
            pbarDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            pbarDialog.setMessage("Reading from Phone Book");
            pbarDialog.setCancelable(false);
            pbarDialog.show();
            pbarDialog.incrementProgressBy(0);         
        }

        @Override
        protected Void doInBackground(Void... params) {
            ReadPhoneBook();
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            pbarDialog.dismiss();
        }

    }    



    private void ReadPhoneBook() {
        int i=0;             
        int count=0;
        //this is a temp function, it just creates buttons, does not read the phone book.
        for (i=0;i<10;i++)
        {           
            b = new Button(this);
            b.setText("Testing");            
            b.setId(1);
            final Activity self = this;
            b.setOnClickListener(new OnClickListener(){
                @Override
                public void onClick(View v){
                    initiateACall(v,self);
                }
            });
            ll.addView(b);  
            if (i%3 == 0){
                pbarDialog.incrementProgressBy(30*i/3);
            }
        }

    }

次のエラーが表示されます。

util.concurrent.FutureTask.run(FutureTask.java:137) 06-24 21:52:36.436: エラー/AndroidRuntime(297): java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068) 06-24 で21:52:36.436: エラー/AndroidRuntime(297): java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561) 06-24 21:52:36.436: エラー/AndroidRuntime(297): Java で.lang.Thread.run(Thread.java:1096) 06-24 21:52:36.436: ERROR/AndroidRuntime(297): 原因: android.view.ViewRoot$CalledFromWrongThreadException: ビュー階層を作成した元のスレッドのみがその見解に触れてください。06-24 21:52:36.436: エラー/AndroidRuntime(297): android.view.ViewRoot.checkThread(ViewRoot.java:2802) で 06-24 21:52:36.436: エラー/AndroidRuntime(297): Android で。 view.ViewRoot.requestLayout (ViewRoot.java:594) 06-24 21:52:36.436:

4

2 に答える 2

0

問題を修正しました。スレッドからメイン アクティビティのビューを更新しようとしていました。

于 2011-07-31T23:36:13.840 に答える
0

これは、Android の AsyncTask クラスを使用してスレッドを統計します。

new TheTask().execute();

ただし、さらに新しいスレッドを見つめている理由がわかりません。

new Thread(new Runnable(){
            public void run(){          
                //Thread.sleep(5*1000);
                ReadPhoneBook();
                pbarDialog.dismiss();
            }           
        }).start(); 

どちらか一方を使用しないのはなぜですか?

于 2011-06-24T21:22:07.757 に答える