Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/218.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android ProgressDialog赢得';即使在执行异步任务时也不会显示_Android_Android Intent_Show_Android Asynctask_Progressdialog - Fatal编程技术网

Android ProgressDialog赢得';即使在执行异步任务时也不会显示

Android ProgressDialog赢得';即使在执行异步任务时也不会显示,android,android-intent,show,android-asynctask,progressdialog,Android,Android Intent,Show,Android Asynctask,Progressdialog,在我的课堂上,主要活动是: @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); switch (requestCode) { case ... case CREDENTIAL_VIEW: new SetStatus

在我的课堂上,主要活动是:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    switch (requestCode) {
    case ...
    case CREDENTIAL_VIEW:
        new SetStatusProgressBar(this).execute();
还有一个嵌套类:

private class SetStatusProgressBar extends AsyncTask<String, Void, Boolean> {
    private ProgressDialog dialog;
    private Main ctx;

    public SetStatusProgressBar(Main ctx) {
        this.ctx = ctx;
        dialog = new ProgressDialog(ctx);
    }

    // progress dialog to show user that contacting server.
    protected void onPreExecute() {
        this.dialog = ProgressDialog.show(ctx, null,
                "Refreshing data from server...", true, false);
    }

    @Override
    protected void onPostExecute(final Boolean success) {
        //...
        //statements that refresh UI
        //...

        if (dialog.isShowing()) {
            dialog.dismiss();
            timerProgressBarStop();
        }
    }

    protected Boolean doInBackground(final String... args) {
        //...
        //statements to download data from server
        //...
        return true;
    }

}
第二个活动以这种方式返回到主活动:

Intent myIntent = new Intent(Main.this, Credentials.class);
startActivityForResult(myIntent, CREDENTIAL_VIEW);
Intent intent = new Intent();
setResult(RESULT_OK, intent);
finish();
我不明白为什么当我从第二个活动导航到主活动时,只有在UI刷新后才会显示ProgressDialog。。。这样,进度对话框仅在屏幕上停留半秒。。。然后躲起来!:(我希望在下载过程中看到顶部的ProgressDialog

请帮忙。
谢谢大家

好的,首先使用getApplicationContext()而不是ctx变量。使用“this”不利于良好的内存。
尝试在onProgressUpdate(…)方法中更新progressDialog。

在调用
新设置StatusProgressBar(this).execute()之后,是否在
案例凭证视图:
中执行任何其他操作;
?是否在启动“进度”对话框后启动第二个活动?在这种情况下,它将被隐藏,因为它在主活动的上下文中运行。尝试使用应用程序上下文。或者在启动第二个活动之前隐藏进度,并在完成后再次显示。@ackio:否,启动后我不会启动第二个活动进度对话框。只有在第二个活动返回时才会启动进度对话框。
this
getApplicationContext()
严格来说不是一回事。同样,使用
this
(正确)根本不会改变内存占用。在这种情况下,有两个活动和使用getApplicationContext()在这里可能会有所帮助,因为我们不需要将其严格绑定到Credentials.class上下文。如何在onProgressUpdate(…)中更新progressDialog?我应该调用哪个方法?我认为可能是这样的:@Override progressUpdate(整型…值){super.onProgressUpdate(值);if(!dialog.isShowing()){dialog.show();}谢谢你的帮助,但是运气不好。我解决了一个问题:我在第二个类中转移了连接到服务器的所有代码,然后返回main(现在只包含刷新UI的语句)。