首页 > 解决方案 > 静态方法的问题:“无法从静态上下文中引用”

问题描述

我在每个活动中都有这个方法(它只是有效),但是我可以制作一个像 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);
    }

标签: javaandroidfunctionandroid-activitystatic

解决方案


只需删除它并添加一个带有参数的 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())




推荐阅读