Reputation: 55
I'm trying to get this api wrapper for android to work.
one of the provided sample methods is:
APIWrapper wrapper = new APIWrapper(context, "YOUR_API_KEY");
Parameters params = new Parameters()
.addSearch("searchQuery")
.addFields("*")
.addOrder("published_at:desc");
wrapper.search(APIWrapper.Endpoint.GAMES, params, new onSuccessCallback(){
@Override
public void onSuccess(JSONArray result) {
// Do something with resulting JSONArray
}
@Override
public void onError(VolleyError error) {
// Do something on error
}
});
I imported Context context, but the problem is that "context" seems to be null no matter what.
How do I get Context context to work correctly?
EDIT: Context context=this; worked.
Though when I want to call the same method from another class . Like this:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
test2 method = new test2();
method.dostuff4(); // call from other class
Context is null again?
Upvotes: 0
Views: 102
Reputation: 55
What confused me was the context.
I had to pass it along for it to work. Problem solved
https://itekblog.com/android-context-in-non-activity-class/
Upvotes: 0
Reputation: 3295
here check this out! its simple injection! but it will do the job for you!
public class MainActivity extends AppCompatActivity{
private Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = this;
Test2 method = new Test2(context);
method.dostuff4(); // call from other class
}
// and here should be the structure of the Test2 class
public class Test2 {
Context context;
public Test2(Context context){
this.context = context;
}
public void dostuff4(){
//here now you can use the context in it
}
}
Upvotes: 1