我有一個簡單的後臺任務來註冊,顯示用戶的進度對話框。問題是,我相信我得到NullPointerException
是因爲在意圖中啓動的第二個活動以某種方式在dialogProgressHide();
方法之前調用,即使該代碼在代碼中首先列出,因此不同的上下文和進度正在返回null導致錯誤。當完成()時使用ProgressDialog時,NullPointerException
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();
}
}
});
我可能是錯的,但這對我有意義,問題是我該如何解決這個問題?我只是希望對話在完成活動之前消失,以便它不會嘗試當進度不再處於上下文中時。
下面是對話的方法,如果需要:
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)
之前空。 isShowing()'對'ProgressDialog'的mDialog'實例進行空檢查 –
該代碼似乎沒問題,請sho記錄堆棧跟蹤和錯誤日誌。 – ElDuderino
如此(如果mDialog.isShowing()|| mDialog!= null)在dialogProgressHide中? – user3460562