StefMa
StefMa

Reputation: 3434

ProgressDialog do not dissmiss() in AsyncTask

I want to show a ProgressDialog in AsyncTask. This run fantastic. But if i call mLoginPD.dissmiss() in onPostExecute() do not run.

The ProgressDialog is always on the screen.

Here is my code:

SherlockActivity mActivity;
ProgressDialog mLoginPD;

public Task_Login(String name, String pass, SherlockActivity activity) {
    this.passwort = pass;
    this.benutzername = name;
    this.mActivity = activity;
}

protected void onPreExecute() {
    super.onPreExecute();

    mLoginPD = new ProgressDialog(mActivity);
    mLoginPD.show(mActivity, "Login", "Logge Spieler ein...");
}

protected void onPostExecute(Void result) {             
    Log.e("hello", "hello");

    mLoginPD.dismiss();
    mLoginPD.cancel();
    if(mLoginPD.isShowing()) {
        mLoginPD.dismiss();
    }
}

onPostExecute() calls. I can see "hello" in LogCat.

(I have doInBackground() but i is irrelevant)

Upvotes: 1

Views: 303

Answers (3)

StefMa
StefMa

Reputation: 3434

The answer from Geobits istn running too. Always show a NullPointerException.

Here is the code to solve my problem:

mLoginPD = new ProgressDialog(mActivity);
mLoginPD.setTitle("Login");
mLoginPD.setMessage("Logge Spieler ein...");
mLoginPD.show();

than i can call mLoginDP.dismiss() or cancel() in onPostExecute()

Upvotes: 0

Geobits
Geobits

Reputation: 22342

The problem is that you're creating two ProgressDialog objects.

This line:

mLoginPD = new ProgressDialog(mActivity);

creates a dialog and assigns it to mLoginPD, but does not show it.

This line:

mLoginPD.show(mActivity, "Login", "Logge Spieler ein...");

creates another dialog and shows that one. The problem is that show() is a static method that creates and shows a dialog all in one. So it's creating a second one separate from mLoginPD which is shown. mLoginPD is never shown, so calling dismiss() or cancel() doesn't do anything.

What you need to do is this:

mLoginPD = ProgressDialog.show(mActivity, "Login", "Logge Spieler ein...");

in place of both those lines. This uses show() to create and show the dialog and assign it to mLoginPD so you can dismiss it later.

Upvotes: 3

Johnny Sørensen
Johnny Sørensen

Reputation: 209

If you're overriding onPreExecute, i dont think you're supposed to call super.onPreExecute()?

Upvotes: 2

Related Questions