jjLin
jjLin

Reputation: 3291

Cannot show a ProgressDialog in AsyncTask

This is my 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();
    }

The GetBlockListAsyncTask:

 public class GetBlockListAsyncTask extends AsyncTask<Object, Boolean, String>{

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();
}

}

It will show error:

'Caused by: java.lang.NullPointerException'
  onPreExecute(GetBlockListAsyncTask.java:101)

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

Upvotes: 1

Views: 401

Answers (1)

twaddington
twaddington

Reputation: 11645

Try adding a public constructor to your AsyncTask that accepts the Activity Context as the first argument:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Create a new AsyncTask with the Activity Context
    AsyncTask task = new GetBlockListAsyncTask(this);

    // Execute the task
    task.execute();
}

public class GetBlockListAsyncTask extends AsyncTask<Object, Boolean, String> {
    private Context activityContext;

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

    //Constructor
    public GetBlockListAsyncTask(Context c) {
        // Store the activity context
        activityContext = c;
    }

    @Override
    protected String doInBackground(Object... params) { 
        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) {
        activityContext.dismissDialog();
    }

    @Override
    protected void onPreExecute() {
        activityContext.initializeDialog();
    }
}

Upvotes: 3

Related Questions