java - 静态方法的问题:“无法从静态上下文中引用”
问题描述
我在每个活动中都有这个方法(它只是有效),但是我可以制作一个像 API 一样的活动并从任何活动中调用这个函数吗?这会是一个好的解决方案吗?而不是在每个活动中都有相同的功能?
我在一个干净的活动中有这个函数,并想从任何活动中调用这个函数。在这些行中,我得到了错误:无法从静态上下文中引用
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);
}
解决方案
只需删除它并添加一个带有参数的 Context 变量:
public static void getMyPoints(Context contex) {
.....
}
所以你用上下文替换它。您的代码将是:
public static void getMyPoints(Context context) {
final String email = SharedPrefManager.getInstance(context).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(context).addToRequestQueue(request);
}
在您的活动中调用此方法:
Activity.getMyPoints(getBaseContext())
推荐阅读
- git - 使用 DoD 通用访问卡 (GIT) 异地访问 Git - Mac
- bash - Bash AWK 变量替换
- java - Java 客户端在通过 F5 BIG-IP 负载平衡器与 IIS 服务器进行 SSL 握手期间接收“连接重置”
- javascript - javascript 'this' 由调用站点确定
- webpack - 94% 资产优化 ERROR Failed to compile with 1 errors
- javascript - RxJS 5.5+ 中带有管道的多播运算符
- django - `django.db.models.Model` 替换了 `django.db.models.base.Model`
- vba - 构造一个输入值,显示值的单元格,但单元格值是别的什么?
- hyperledger-fabric - 我在 CouchDB 中对目录进行卷,第二次运行“composer network start”时出错
- linux - 奇偶校验升级错误奇偶校验:/lib/x86_64-linux-gnu/libcrypto.so.1.0.0:未找到版本“OPENSSL_1.0.2”(奇偶校验需要)