Android – Method getactivity() does not define type mainactivity
•
Android
I'm trying to use this demo. When I pull it into my mainactivity, I receive the error "method getactivity() does not define type mainactivity"
Createrequestsuccesslistener() and createrequesterrorlistener() encountered 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.tool@R_216_2419@.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;
}
}
resolvent:
Try the following code: –
getActivity()
Change to
MainActivity.this
You must pass an activity instance
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码