我有一個啓動畫面,運行從API下載數據的asyncTask
。在該任務的OnPostExecute
上,我運行下一個asyncTask
發送存儲的電子郵件。一旦完成,我需要一個AlertDialog
彈出一個確定的按鈕,以便用戶知道下載完成。我用這太問題得到儘可能我:爲什麼我會在asyncTask中使用AlertDialog獲取NullPointerException?
Android AlertDialog inside AsyncTask
現在我得到一個NullPointerException當我嘗試添加屬性對話框:
public class JSONParser extends AsyncTask<String, String, JSONObject> {
Context c;
public JSONParser(int api,Context c) {
this.api= api;
this.c = c;
}
...
protected void onPostExecute(JSONObject result) {
JSONObject output = new JSONEmailParser(c).executeOnExecutor(AsyncTask.SERIAL_EXECUTOR, new String[] {null,null,null,null}).get();
}
}
public class JSONEmailParser extends AsyncTask<String, String, JSONObject> {
Context c;
AlertDialog.Builder builder;
public JSONEmailParser(Context c){
this.c = c;
}
protected void onPreExecute(int api){
builder = new AlertDialog.Builder(SplashScreen.this);
}
...
protected void onPostExecute(JSONObject result) {
setLastUpdate();
builder.setTitle("Sales Toolkit");
builder.setCancelable(false);
builder.setMessage("Download Complete");
builder.setPositiveButton("Continue", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
dialog.dismiss();
endSplash();
}
});
builder.show();
}
}
錯誤來了builder.setTitle("Sales Toolkit");
您應該始終使用@Override註釋。它有助於捕捉這樣的錯誤。 – JDJ