1

I'm new to android and I have a problem with this code. I'm trying to get a JSON String and start another activity to display it as a ListView.
I'm not able to start the activity. It says that the The constructor Intent(RequestJsonString, Class) is undefined and The constructor Intent(RequestJsonString, Class) is undefined .

Here: Intent intent = new Intent(RequestJsonString.this,DisplayResults.class); and Here: RequestJsonString.this.startActivity(intent);

I have read many posts on this on stackoverflow and tried with activity, context and this. But still I'm not getting it right. I think I should be missing something. Any help is appreciated.

public class RequestJsonString extends AsyncTask<String, Void, JSONObject> {

@Override
protected JSONObject doInBackground(String... urls) {
    // Code HTTP Get Request and get JSONObject
            return jsonObject;
}

protected void onPostExecute(JSONObject jsonObj){
    try {

        Intent intent = new Intent(RequestJsonString.this,DisplayResults.class);
        intent.putExtra("JSON_Object", jsonObj.toString());
        RequestJsonString.this.startActivity(intent);

    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    Log.v("Json_OutPut","Done");

}

}
Crocode
  • 2,796
  • 6
  • 24
  • 29

2 Answers2

2

To start the activity from AsyncTask.

Intent intent = new Intent(YourActivityName.this,DisplayResults.class);

or you can do same like below.

Declare the context instance variable and initialize it in onCreate method.

private Context context;
public void onCreate(Bundle bundle) {
   ............
   context = this;
   ........
}

Start the activity like this.

Intent intent = new Intent(context,DisplayResults.class);
intent.putExtra("JSON_Object", jsonObj.toString());
startActivity(intent);
Ajay S
  • 45,716
  • 27
  • 84
  • 103
1

In your case you are referring to asynctask class context

Intent intent = new Intent(RequestJsonString.this,DisplayResults.class);

Use a Activity Context

Intent intent = new Intent(ActivityName.this,DisplayResults.class);

Check the link to know when to use getApplicationContext() and when to use Activity Context

When to call activity context OR application context?

Edit:

Pass the Activity context to the asynctask constructor

 new RequestJsonString(ActivityName.this).execute(params..);

In your asynctask constructor

 Context c;
 public  RequestJsonString( Context context)
 {
        c= context;
 }

Then

   Intent intent = new Intent(c,DisplayResults.class);
   startActivity(intent);
Community
  • 1
  • 1
Raghunandan
  • 129,147
  • 24
  • 216
  • 249
  • The constructor Intent(RequestJsonString, Class) is undefined – Crocode Apr 21 '13 at 06:58
  • 1
    pass the context to the asynctask constructor and use the same. check the edit above. – Raghunandan Apr 21 '13 at 07:01
  • `public static void mygetJSONfromURL(String url){ //initialize new RequestJsonString(MainActivity.this).execute(url); } ` – Crocode Apr 21 '13 at 07:08
  • This is in MainActivity extends Activity – Crocode Apr 21 '13 at 07:09
  • 1
    remove the staticand try public void mygetJSONfromURL(String url){ //initialize new RequestJsonString(MainActivity.this).execute(url); } – Raghunandan Apr 21 '13 at 07:10
  • Could you please explain why RequestJsonString.this wasn't working? – Crocode Apr 21 '13 at 07:13
  • 1
    RequestJsonString.this refers to your asynctask context. You should refer to the activity context since your class extends activity. If you need to startactivity from a non activity class, then you need the activity context and you can pass the same to the constructor of the non activity class. Check the link in the answer. – Raghunandan Apr 21 '13 at 07:15
  • Great! Thanks again for helping. – Crocode Apr 21 '13 at 07:16
  • Shouldn't it be c.startActivity(intent)? – Crocode Apr 21 '13 at 07:19
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/28587/discussion-between-raghunandan-and-crocode) – Raghunandan Apr 21 '13 at 07:20