Reputation: 4253
I'm new on android studio, probably it is a simple mistake. I want to set a color after a volley request, but my color variable is null. I'm sure volley is returning the color. why my color is null after my volley request?
I have this code:
public class MyClass extends AppCompatActivity {
private static Context context;
public static String color;
@Override
protected void onCreate(Bundle savedInstanceState) {
this.context = getApplicationContext();
getColor();
...
}
public void getColor() {
<--volley Queue-->
StringRequest strReq = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
color = response; // ??????????????????????????
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
//Log.d("Error.Response", response);
}
}
);
mRequestQueue.add(strReq);
Log.w("erro: ", color); // color here is null?????????????? why??
}//getcolor()
}class
should I use get/set? anyone could give me an example?
Upvotes: 0
Views: 458
Reputation: 43738
This is an asynchronous call. The log statement will execute before the result is delivered to the listener. Therefore, if you inspect the variable immediately after the call it will most likely be still null
.
Upvotes: 3