Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
I'm trying to use the demo how ever when I have pulled it in to my MainActivity I am getting the error "The method getActivity() is undefined for the type MainActivity"
createRequestSuccessListener() and createRequestErrorListener() also got the same problem
package com.example.testgetanik;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import com.android.volley.Request.Method;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.Volley;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import com.android.volley.Response.ErrorListener;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Map<String, String> params = new HashMap<String, String>();
params.put("id","1");
params.put("name", "myname");
String url = "http://api.rottentomatoes.com/api/public/v1.0/movies/770672123/cast.json?apikey=3p9ehnhzbxwpbd6mk8fncf67";
RequestQueue requestQueue = Volley.newRequestQueue(getActivity());
CustomRequest jsObjRequest = new CustomRequest(Method.POST,url,params,this.createRequestSuccessListener(),this.createRequestErrorListener());
requestQueue.add(jsObjRequest);
URL urlToRequest = new URL(url);
WebServiceAsyncTask asyncTask = new WebServiceAsyncTask();
asyncTask.execute(urlToRequest);
}catch(MalformedURLException e){
e.printStackTrace();
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
If your class extends Activity
then you should use this
or activityname.this
or getApplicationContext
to get the context.
if it extends from Fragment
then you should use getActivity()
.
private Response.Listener<JSONObject> createRequestSuccessListener(){
return new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d(Constants.TAG, "===> Response => "+response.toString());
private Response.ErrorListener createRequestErrorListener(){
return new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d(Constants.TAG, "===> JsonObjectRequest error ");
Log.d(Constants.TAG, error.toString());
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.