KryNaC
KryNaC

Reputation: 379

Static call for startActivity from AsyncTask

I have implemented a FragmentPagerAdapter of 4-lashes, and in each of them I load a fragment with a different view. In one of them, pressing an image executed a AsyncTask to obtain a series of data from a server and loads a new class through an intent on the onPostExecute() method.

I had this functionality in one activity and worked perfectly. Now to make the call from the fragment I have to make calls using a static mode of this class and I get error in the line of code 'startActivity(i)':

  //AsyncTask 
private static class CargarJSON extends AsyncTask<String, Void, String> {
    Context mContext;
    public CargarJSON(Context context) {
        mContext = context;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        mProgressItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
        mProgressItem.setVisible(true);
        mProgressItem.setActionView(R.layout.actionbar_indeterminate_progress);
        mProgressItem.expandActionView();
    }

    @Override
    protected String doInBackground(String... params) {                 
        String url = params[0];              
        String data = MetodosJSON.getHttpResponse(url);
        MetodosJSON.parseaJSON2(data, IniSelCategoria.ac);  
        return params[1];                   
    }

    @Override
    protected void onPostExecute(String titulo) {   
        super.onPostExecute(titulo);
        // start new activity           
        Intent i = new Intent(mContext, PantallaInfo.class);
        i.putExtra("title", titulo);
        i.putExtra("URLser", urlSer);
        **startActivity(i);**
        mProgressItem.setVisible(false);
    }

}

The mistake is:

Cannot make a static reference to the non-static method startActivity(Intent) from the type Activity

How do I make the method call 'startActivity(i)'?

Thank you very much.

Upvotes: 3

Views: 2360

Answers (2)

AAnkit
AAnkit

Reputation: 27549

Change your code with the below one.

        Intent i = new Intent(mContext, PantallaInfo.class);
        i.putExtra("title", titulo);
        i.putExtra("URLser", urlSer);
        mContext.startActivity(i); // call using Context instance

Upvotes: 2

codeMagic
codeMagic

Reputation: 44571

Change it to

mContext.startActivity(i);

You need to use a context to call that method if not calling from an Activity. Luckily you are already passing a Context to the constructor.

Upvotes: 6

Related Questions