user3660001
user3660001

Reputation: 61

The method getActivity() is undefined for the type MainActivity

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);
        try{
            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;
    }

}

Upvotes: 6

Views: 17460

Answers (4)

raju_kr
raju_kr

Reputation: 143

        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());
                }
            };
        }

Upvotes: 0

David Corsalini
David Corsalini

Reputation: 8208

Activity is a Context, so when you need a Context inside Activity, just use this. If you need a Context inside a Fragment, use getActivity().

Upvotes: 7

Spring Breaker
Spring Breaker

Reputation: 8251

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().

Upvotes: 6

duggu
duggu

Reputation: 38439

Try below code:-

getActivity()

change to

MainActivity.this

You have to pass activity instance.

Upvotes: 14

Related Questions