错误无法添加窗口 - 令牌null不适用于应用程序

时间:2012-04-05 12:34:58

标签: android progress-bar

活动显示带有正按钮的对话窗口。点击按钮,它将转到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));
    }
}

1 个答案:

答案 0 :(得分:15)

progressBar = new ProgressDialog(getApplicationContext());

代替getApplicationContext()传递你的ActivityName.this或getContext()