【发布时间】:2020-05-11 09:57:23
【问题描述】:
我在每个活动中都有这个方法(它只是有效),但我可以制作一个像 API 一样的 1 活动并从任何活动中调用这个函数吗?这会是一个好的解决方案吗?而不是在每个活动中都有相同的功能?
我在一个干净的活动中有这个函数,并想从任何活动中调用这个函数。 在这些行中,我得到了错误:无法从静态上下文中引用
final String email = SharedPrefManager.getInstance(this).getUserEmail();
final TextView bottom_bar_points = findViewById(R.id.bottom_bar_points);
...
MySingleton.getmInstance(this).addToRequestQueue(request);
我很想看到一些建议,在此先感谢。
代码
public static void getMyPoints() {
final String email = SharedPrefManager.getInstance(this).getUserEmail();
final TextView bottom_bar_points = findViewById(R.id.bottom_bar_points);
String uRl = "https://mywebsite.com/getmypoints.php";
StringRequest request = new StringRequest(Request.Method.POST, uRl, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
if (response.equals("Found")) {
bottom_bar_points.setText(response);
return;
} else {
bottom_bar_points.setText(response);
return;
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
bottom_bar_points.setError(error.toString());
return;
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
HashMap<String, String> param = new HashMap<>();
param.put("email", email);
return param;
}
};
request.setRetryPolicy(new DefaultRetryPolicy(30000, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
MySingleton.getmInstance(this).addToRequestQueue(request);
}
【问题讨论】:
标签: java android function android-activity static