简体   繁体   中英

Using ProgressDialog when finish() on Activity, NullPointerException

I have a simple background task to sign up, displaying a progress dialog for the user. The issue is that I believe I'm getting NullPointerException is because the second activity that's being launched in the intent is somehow being called before the dialogProgressHide(); method, even though that one is listed first in the code, therefore different context and the progress is returning null causing error.

book.saveInBackground(new SaveCallback()
                {

                    @Override
                    public void done(ParseException e) {
                        if (e == null) 
                        {

                            dialogProgressHide();
                            Toast.makeText(getApplicationContext(), "Your book was posted correctly!", Toast.LENGTH_LONG).show();
                            finish();
                        } 
                        else 
                        {

                            Toast.makeText(getApplicationContext(), "An error has occured: " + e +  "\n" + "Please try again!", Toast.LENGTH_LONG).show();
                            dialogProgressHide();
                        }
                    }
                });

I could be wrong but that makes sense to me, question is how would I fix this? I simply want the dialog to disappear before finishing the activity, so that it doesn't attempt to when the progress is no longer in context.

Here is dialog methods if needed:

  private void dialogProgressShow() {
            mDialog = new ProgressDialog(this);
            mDialog.setMessage("One moment please while we post your book.");
            mDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);

            mDialog.show();
        }
        private void dialogProgressHide() {
            if (mDialog.isShowing())
            {
                mDialog.dismiss();
                mDialog = null;
            }
        }
    }

LOGCAT:

03-25 17:12:44.862: E/AndroidRuntime(16528): FATAL EXCEPTION: main
03-25 17:12:44.862: E/AndroidRuntime(16528): Process: com.jameswilson.booksale, PID: 16528
03-25 17:12:44.862: E/AndroidRuntime(16528): java.lang.NullPointerException
03-25 17:12:44.862: E/AndroidRuntime(16528):    at com.jameswilson.booksale.PostBookActivity.dialogProgressHide(PostBookActivity.java:255)
03-25 17:12:44.862: E/AndroidRuntime(16528):    at com.jameswilson.booksale.PostBookActivity.access$1(PostBookActivity.java:254)
03-25 17:12:44.862: E/AndroidRuntime(16528):    at com.jameswilson.booksale.PostBookActivity$2$1.done(PostBookActivity.java:144)
03-25 17:12:44.862: E/AndroidRuntime(16528):    at com.parse.SaveCallback.internalDone(SaveCallback.java:39)
03-25 17:12:44.862: E/AndroidRuntime(16528):    at com.parse.SaveCallback.internalDone(SaveCallback.java:27)
03-25 17:12:44.862: E/AndroidRuntime(16528):    at com.parse.Parse$6$1.run(Parse.java:846)
03-25 17:12:44.862: E/AndroidRuntime(16528):    at android.os.Handler.handleCallback(Handler.java:733)
03-25 17:12:44.862: E/AndroidRuntime(16528):    at android.os.Handler.dispatchMessage(Handler.java:95)
03-25 17:12:44.862: E/AndroidRuntime(16528):    at android.os.Looper.loop(Looper.java:136)
03-25 17:12:44.862: E/AndroidRuntime(16528):    at android.app.ActivityThread.main(ActivityThread.java:5017)
03-25 17:12:44.862: E/AndroidRuntime(16528):    at java.lang.reflect.Method.invokeNative(Native Method)
03-25 17:12:44.862: E/AndroidRuntime(16528):    at java.lang.reflect.Method.invoke(Method.java:515)
03-25 17:12:44.862: E/AndroidRuntime(16528):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
03-25 17:12:44.862: E/AndroidRuntime(16528):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
03-25 17:12:44.862: E/AndroidRuntime(16528):    at dalvik.system.NativeStart.main(Native Method)

Check if your dialog reference is null before trying to dismiss it

private void dialogProgressHide() {
    if (mDialog != null && mDialog.isShowing())
    {
        mDialog.dismiss();
        mDialog = null;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM