Cannot show a ProgressDialog in AsyncTask
<p>This is my code:</p>

<pre><code>@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.i(TAG, "onCreate");
        setContentView(R.layout.list);    
        new GetBlockListAsyncTask().execute(BlockListActivity.this);
    }
public void initializeDialog() {    
        dialog = ProgressDialog.show(BlockListActivity.this, "", "Loading data. Wait...", true);
        dialog.show();
    }

    public void dismissDialog(){        
        dialog.dismiss();
    }
</code></pre>

<p>The GetBlockListAsyncTask:  </p>

<pre><code> public class GetBlockListAsyncTask extends AsyncTask&lt;Object, Boolean, String&gt;{

private BlockListActivity callerActivity;
private String TAG = "GetBlockListAsyncTask";
private String stringCode = "";

@Override
protected String doInBackground(Object... params) { 

    callerActivity = (BlockListActivity)params[0];      
    try {
        Log.d(TAG, "Start to sleep");
        Thread.sleep(4000);         
        Log.d(TAG, "End sleep");
    } catch (InterruptedException e) {
       e.printStackTrace();
    }
    return null;
}

@Override
protected void onPostExecute(String response) {
    callerActivity.dismissDialog();
}

@Override
protected void onPreExecute() {
    callerActivity.initializeDialog();
}
</code></pre>

<p>}</p>

<p>It will show error: </p>

<pre><code>'Caused by: java.lang.NullPointerException'
  onPreExecute(GetBlockListAsyncTask.java:101)
</code></pre>

<p>I find a solution is that if I move the initializeDialog out of the AsyncTask and put it before the line <code>new GetBlockListAsyncTask().execute(BlockListActivity.this);</code> in <code>onCreate</code>, it works.<br>
The question is how to make it work if I want to put the initializeDialog in the AsyncTask .</p>
