0

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

我做了什么——“代码

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() 中调用的函数(在不同的类中)中使用,该函数也从应用程序周围的其他活动中调用

4

3 回答 3

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;
        }
    }
于 2013-06-18T07:49:09.250 回答
0

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

尝试执行onPreExecute()在 UI 线程上调用的初始化,并且仅在doInBackground().

于 2013-06-18T07:50:21.503 回答
-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
            }
        }
于 2013-06-18T07:47:07.697 回答