22

I have an AsyncTask, that fills a custom List with parsed data from Internet.

In PostExecute I fill that List and get it ready to transfer it to a new Activity.

I do it this way:

@Override
protected void onPostExecute(List<VideoDataDescription> result) 
{
    super.onPostExecute(result);
    MainActivity.progressDialog.dismiss();

    context.startActivity(new Intent(context, ResultsQueryActivity.class));


}

where context

    private Context context;

In LogCat after executing this code I get a Java.lang.NullPointerException. Is this possible and correct to start an Activity as I do it?

UPD I have added

    private Context mContext;


public YoutubeAndYahooParser(Context context) 
{
    super();
    this.mContext = context;
}

to initialize context and call

YoutubeAndYahooParser youtubeAndYahooParser = new YoutubeAndYahooParser(ResultsQueryActivity.this);
                    youtubeAndYahooParser.execute("my string to pass in asynctak");

After this in PostExecute

Intent intent = new Intent(mContext, ResultsQueryActivity.class);
 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
 mContext.startActivity(intent);    

I added new flag because of I have got in LogCat the next:

*Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?*

Am I right?

2
  • 2
    how have you initialized context?
    – Vladimir
    Feb 2, 2012 at 18:47
  • Seems to be, no. How to do initialize context? Feb 2, 2012 at 19:04

3 Answers 3

44

You should pass in the application context rather than a context from the local activity. I.e. use context.getApplicationContext() and save that in a local variable in your AsyncTask subsclass.

The code might looks something like this:

public class MyAsyncTask extends AsyncTask {

    Context context;
    private MyAsyncTask(Context context) {
        this.context = context.getApplicationContext();
    }

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

    @Override
    protected void onPostExecute(List<VideoDataDescription> result) {
        super.onPostExecute(result);
        MainActivity.progressDialog.dismiss();

        context.startActivity(new Intent(context, ResultsQueryActivity.class));
    }
}

you'd call it like this:

   new MyAsyncTask(context).execute();
6
  • Please, can you add come code how to do it? I updated topicfor details. Feb 2, 2012 at 20:52
  • 1
    On new MyAsyncTask(context).execute(); where does context come from?
    – dotty
    Jul 2, 2012 at 11:50
  • 12
    you also need to set the flag activity new task - else you will get a runtime exception: Intent i = new Intent(context, MyActivity.class); i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(i); Jul 20, 2012 at 8:40
  • 1
    A reference related with the @crazyhorse's comment: stackoverflow.com/q/3689581/842697 Jun 13, 2013 at 12:15
  • 1
    @dotty YourClassName.this should be instead of context. Jun 4, 2014 at 21:19
18

I tried this just now ... it works in PostExecute Method!!!

Intent intent_name = new Intent();
intent_name.setClass(getApplicationContext(),DestinationClassName.class);
startActivity(intent_name);
0

But its better if you start a new Intent Based on the response(result) obtained from the previous activities.

This will eliminate the possibility of the error response from invoking the new intent.

Example if the previous activity was supposed to return Succesfully... or Welcome to allow the new intent to start, the i could check it out in this way

  protected void onPostExecute(String result) {
       if (result.equals("Succesfully...")){
           context.startActivity(new Intent(context, Login_Activity.class));
           Toast.makeText(context, result, Toast.LENGTH_LONG).show();


       }else  if (result.contains("Welcome")){
           context.startActivity(new Intent(context, MainActivity.class));
       }else {
           Toast.makeText(context,result,Toast.LENGTH_LONG).show();
       }

    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.