3

ポジティブボタン付きのダイアログウィンドウを表示するアクティビティです。ボタンをクリックすると、Staticdisplayアクティビティに移動します。アクティビティの読み込み中にプログレスバーを追加しようとしています。次のエラーが発生します。$BadTokenException: Unable to add window -- token null is not for an application

これはプロセスバーを追加するためのコードですprogressBar.show();。この行でエラーが発生しています。このエラーから抜け出すにはどうすればよいですか。ありがとう。

AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setCustomTitle(title);

        builder.setMessage(R.string.app_description).setPositiveButton(
                "Ok", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        if (Registration.isRunning == false) {
                            startService(new Intent(
                                    getApplicationContext(),
                                    Registration.class));
                        }
                        staticInfo();
                        if (Registration.ruid == null)
                            Registration.ruid = uId;
                        progressBar = new ProgressDialog(
                                getApplicationContext());
                        progressBar.setCancelable(true);
                        progressBar.setMessage("Loading Activity...");
                        progressBar
                                .setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                        progressBar.setProgress(0);
                        progressBar.setMax(100);
                        progressBar.show();
                        progressBarStatus = 0;

                        new Thread(new Runnable() {
                            public void run() {
                                while (progressBarStatus < 100) {

                                    progressBarStatus = 100;

                                    progressBarHandler.post(new Runnable() {
                                        public void run() {
                                            progressBar
                                                    .setProgress(progressBarStatus);
                                        }
                                    });
                                }

                                if (progressBarStatus >= 100) {

                                    progressBar.dismiss();
                                    startActivity(new Intent(
                                            getApplicationContext(),
                                            StatisticDisplay.class));
                                }
                            }
                        }).start();

                    }

                });
        AlertDialog alert = builder.create();
        alert.show();
    } else {
        startActivity(new Intent(getApplicationContext(),
                StatisticDisplay.class));
    }
}
4

1 に答える 1

15
progressBar = new ProgressDialog(getApplicationContext());

getApplicationContext() の代わりに ur ActivityName.this または getContext() を渡します

于 2012-04-05T12:38:53.643 に答える