3

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.

KryNaC
  • 379
  • 4
  • 21

2 Answers2

6

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.

codeMagic
  • 44,549
  • 13
  • 77
  • 93
  • Ohhhh THANK YOU VERY MUCH !!! I had long been trying to fix this. I owe you a beer. =D – KryNaC Oct 01 '13 at 17:21
  • Sounds like a deal! Glad I could help – codeMagic Oct 01 '13 at 17:24
  • @codeMagic i did mention the same to his previous question http://stackoverflow.com/questions/19120205/intent-in-asynctask-for-fragmentpageradapter. took me more than 10 comments but still op could not get his problem solved. – Raghunandan Oct 01 '13 at 17:53
  • @codeMagic i was wondering what i missed. i got fed up commenting more than 10 times. – Raghunandan Oct 01 '13 at 18:05
  • @Raghunandan I can understand that. I didn't see where you explicitly said to use `Context` with `startActivity()` so it looks to me like that's all that was missing. You seem to have gotten him mostly there so it should have been accepted. But that was a lot of code to look at with little explanation. – codeMagic Oct 01 '13 at 18:16
  • @codeMagic i did mention "Also use mContext to startActivity". any how that's irrelevant now. But yes i could have explained it better – Raghunandan Oct 01 '13 at 18:19
  • @Raghunandan Oh, I see now. I thought that was dealing with the same classes but looks like that post is using a different class but similar problem. Without knowing what the problem is now its hard to say because it looks like you fixed a lot of issues there. – codeMagic Oct 01 '13 at 18:28
  • @Raghunandan thanks for your clarification but I was lost and did not know what you want said me, so I asked you to write me the code and you never wrote me "mContext.startActivity (i)" like whether 'codeMagic' mentioned me . 'Raghunandan' Anyway, I appreciate your dedication and interest because otherwise there would not have been understood, although in the end you dont follow answering me. – KryNaC Oct 01 '13 at 18:37
  • @KryNaC i was fed up commenting over and over again. yes may be i should have put that line i missed. Sorry my bad. glad you got your problem sovled. credits to codeMagic – Raghunandan Oct 01 '13 at 18:39
  • @KryNaC check his edit. He did answer it just didn't actually write that part of the code because thought that part was understood but has edited now. You could still accept that answer if all the problems are figured out so it may help others because he did fix several issues it looks like – codeMagic Oct 01 '13 at 18:41
2

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
AAnkit
  • 27,299
  • 12
  • 60
  • 71