显示进度条时无法添加窗口异常

时间:2013-06-18 07:41:17

标签: android listview android-progressbar

AIM 我有一个带有列表视图和一些按钮以及一些额外内容的活动。我想要的是在活动开始时使用自定义适配器填充此列表视图。由于生成这个自定义适配器需要时间,我想添加一个进度条,这样用户就不会被迫盯着无响应的屏幕。完成后,关闭此进度条并按预期显示活动。

我说的是什么 - “ CODE

public void onCreate(Bundle savedInstanceState) {
...
// all initialization done here properly
new ProgressTask(this).execute("");
}

public class ProgressTask extends AsyncTask<String, Void, Boolean> {

    public ProgressTask(ScanInboxActivity activity) {
        dialog = new ProgressDialog(activity.getApplicationContext());
        dialog.setIndeterminate(true);
        dialog.setCancelable(false);
    }

        /** progress dialog to show user that the backup is processing. */
    private ProgressDialog dialog;

    protected void onPreExecute() {
        this.dialog.setMessage("Scan start");
        this.dialog.show();
    }

    @Override
    protected void onPostExecute(final Boolean success) {
        if (dialog.isShowing()) {
            dialog.dismiss();               
        }
        populateList();
    }

    protected Boolean doInBackground(final String... args) {
        init(); // tasks to be performed- no error here
        scanSms(); // tasks to be performed- no error here
        return true;
    }
}

我得到以下例外:

FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example/com.example.ScanInboxActivity}: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
at android.app.ActivityThread.access$2300(ActivityThread.java:125)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4627)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
at android.view.ViewRoot.setView(ViewRoot.java:509)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
at android.app.Dialog.show(Dialog.java:241)
at com.example.ScanInboxActivity$ProgressTask.<init>(ScanInboxActivity.java:212)
at com.example.ScanInboxActivity.onCreate(ScanInboxActivity.java:59)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
... 11 more

请注意,当我写这个

public void onCreate(Bundle savedInstanceState) {
// all initialization done here properly
init();
scanSms();
populateList();
}

事情很好,虽然开始这项活动的活动暂停了一会儿。我不希望这种情况发生。

请建议做什么。提前谢谢。

编辑 scanSms()使用上下文。我无法消除上下文的使用,因为它在scanSms()中调用的函数(在不同的类中)中使用,也可以从应用程序周围的其他活动中调用

3 个答案:

答案 0 :(得分:0)

试试这个:

public ProgressTask(ScanInboxActivity activity) {
        dialog = new ProgressDialog(activity);
        dialog.setIndeterminate(true);
        dialog.setCancelable(false);
    }

    protected void onPreExecute() {
        this.dialog.setMessage("Scan start");
        this.dialog.show();
    }

编辑:

public class ProgressTask extends AsyncTask<String, Void, Boolean> {

        /** progress dialog to show user that the backup is processing. */
        private ProgressDialog dialog;

        public ProgressTask(MainMenuActivity activity) {
            dialog = new ProgressDialog(activity);
            dialog.setIndeterminate(true);
            dialog.setCancelable(false);
        }

        protected void onPreExecute() {
            this.dialog.setMessage("Scan start");
            this.dialog.show();
        }

        @Override
        protected void onPostExecute(final Boolean success) {
            if (dialog.isShowing()) {
                dialog.dismiss();               
            }
            //populateList();
        }

        protected Boolean doInBackground(final String... args) {
            //init(); // tasks to be performed- no error here
            //scanSms(); // tasks to be performed- no error here
            try {
                Thread.sleep(10000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return true;
        }
    }

答案 1 :(得分:0)

我的假设是您放入init()的方法scanSms()doInBackground()会使用Context。如果是这样,那么这可能是原因,因为doInBackground()不应该使用上下文。

尝试在UI线程上调用的onPreExecute()中进行初始化,并且只在doInBackground()中进行重要的事情。

答案 2 :(得分:-2)

  class asytask extends AsyncTask<String, Integer, String>
        {
            ProgressDialog dialog ;
            @Override
            protected void onPreExecute() {
                // TODO Auto-generated method stub
                super.onPreExecute();
                dialog  = new ProgressDialog(getApplicationContext());
                dialog.show();
            }
            @Override
            protected String doInBackground(String... params) {
                // TODO Auto-generated method stub

//use your backgournd process
                return null;
            }
            @Override
            protected void onPostExecute(String result) {
                // TODO Auto-generated method stub
                super.onPostExecute(result);
                dialog.dismiss();

 //  visible process
            }
        }