为什么 Android Studio 提示“Method doesn't override its superclass”?

Why Android Studio prompt “Method doesn't override its superclass”?

我想使用 volley 建立带身份验证的 http 连接。在 this answer 之后我添加了片段

  @Override
                    public Map<String, String> getHeaders() throws AuthFailureError {
                        HashMap<String, String> params = new HashMap<String, String>();
                        String creds = String.format("%s:%s","USERNAME","PASSWORD");
                        String auth = "Basic " + Base64.encodeToString(creds.getBytes(), Base64.DEFAULT);
                        params.put("Authorization", auth);
                        return params;
                    }

在 Anonymous Inner Class StringRequest 中,它看起来像:

StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
        new Response.Listener<String>() {


//the segment below is what I add 
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                HashMap<String, String> params = new HashMap<String, String>();
                String creds = String.format("%s:%s","USERNAME","PASSWORD");
                String auth = "Basic " + Base64.encodeToString(creds.getBytes(), Base64.DEFAULT);
                params.put("Authorization", auth);
                return params;
            }

//the segment above is what I add 
            @Override
            public void onResponse(String response) {
                // Display the first 500 characters of the response string.
            }
        }, new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {
    }
});

但是,IDE 暗示 getHeaders() 不会覆盖它的超级 class。

为什么?我发现StringRequest extends class Request<String>,后者确实有一个方法叫做getHeaders().

您正在 Response.Listener<String> 的新实例中覆盖 public Map<String, String> getHeaders() 而不是 Request<String> 并且 Response.Listener<String> 没有这样的方法(只有 onResponse(String))。