【问题标题】:ProgressDialog new Activity Asynctask is not showing, why?ProgressDialog new Activity Asynctask 没有显示,为什么?
【发布时间】:2016-11-17 10:41:51
【问题描述】:

我用下面的代码做了一个 AsyncTask 类

public class removeDialog extends AsyncTask<Void, Void, Void> {

Context c;
ProgressDialog asyncDialog;
String page;

public removeDialog(Context c, String page) {
    this.c = c;
    this.page = page;

    asyncDialog = new ProgressDialog(c);
}

@Override
protected void onPreExecute() {
    //set message of the dialog
    asyncDialog.setTitle("Please wait");
    asyncDialog.setMessage("Loading...");
    asyncDialog.setCancelable(false);
    //show dialog
    asyncDialog.show();

    if (page == "algemeneVoorwaarden") {
        Intent intent = new Intent(c, algemeneVoorwaarden.class);
        c.startActivity(intent);
    }
    if (page == "contact") {
        Intent intent = new Intent(c, contactTest.class);
        c.startActivity(intent);
    }

    super.onPreExecute();
}

@Override
protected Void doInBackground(Void... arg0) {

    //don't touch dialog here it'll break the application
    //do some lengthy stuff like calling login webservice

    return null;
}

@Override
protected void onPostExecute(Void result) {
    //hide the dialog
    asyncDialog.dismiss();

    super.onPostExecute(result);
}
}

我第一次尝试: 第一次看到 ProgressDialog,但第二次我想打开 Activity 时却一无所获。

我第二次尝试: 我第一次尝试时也没有 ProgressDialog。

我在 AsyncTask 类中执行我的代码,代码:

voorwaarden.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new removeDialog(c, "algemeneVoorwaarden").execute();
            }
        });

有人知道为什么它不起作用吗?请帮帮我。

【问题讨论】:

  • removeDialog 是您的 AsyncTask 派生类的名称吗?如果你能发布你的班级的标题会很好。
  • 是类名。
  • 好的。好吧,在您的onPreExecute 中,您正在显示对话框并开始一个新活动。我可能是错的,但我认为你的对话框显示在第一个活动中,所以当你开始一个新的活动时,你的对话框被留下了。
  • 嗯,你知道怎么做吗?
  • 我尝试了:ThreadSleep,我得到了一些进一步的结果。 ProgressDialog 正在后台运行,但我不知道如何修复它。

标签: java android-activity android-asynctask progressdialog


【解决方案1】:

您的对话框将在显示后立即消失,因为您的doInBackground 为空。尝试在几秒钟后添加Thread.sleep(),只是为了模拟延迟。

另外,我怀疑你开始的新活动会留下你的对话。所以我建议你现在在没有这些新活动的情况下测试代码。

public class RemoveDialog extends AsyncTask<Void, Void, Void> {

    ProgressDialog asyncDialog;

    public RemoveDialog(Context c) {
        asyncDialog = new ProgressDialog(c);
    }

    @Override
    protected void onPreExecute() {
        //set message of the dialog
        asyncDialog.setTitle("Please wait");
        asyncDialog.setMessage("Loading...");
        asyncDialog.setCancelable(false);

        //show dialog
        asyncDialog.show();

        super.onPreExecute();
    }

    @Override
    protected Void doInBackground(Void... arg0) {

        try {
            Thread.sleep(3000);
        }
        catch (InterruptedException ex) {
            ex.printStackTrace();
        }

        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        //hide the dialog
        asyncDialog.dismiss();

        super.onPostExecute(result);
    }
}

【讨论】:

    猜你喜欢
    • 2012-08-13
    • 1970-01-01
    • 2014-07-25
    • 2012-08-13
    • 2017-06-01
    • 2015-02-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多