简体   繁体   中英

Override method implemented in anonymous class not recognized

I have the following code, as taken from this tutorial: http://developer.android.com/training/volley/simple.html

        final TextView mResponse = (TextView) findViewById(R.id.response);
        RequestQueue queue = Volley.newRequestQueue(this);
        String url = "http://www.example.com/";

// Request a string response from the provided URL.
        StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
                new Response.Listener() {
                    @Override
                    public void onResponse(String response) {
                        // Display the first 500 characters of the response string.
                        mResponse.setText("Response is: "+ response.substring(0,500));
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                mResponse.setText("That didn't work!");
            }
        });

However, despite the implementation of the onResponse() method within the anonymous Listener class, Response.Listener is being underlined in red, giving the following error: "'Anonymous class derived from Listener' must either be either be declared abstract or implement abstract method 'onResponse(T)' in 'Listener'". I'm not exactly sure why it isn't seeing that I've done exactly that.

Response.Listener is a generic type. You have to specify:

new Response.Listener<String>()
compile 'dev.dworks.libs:volleyplus:+'
compile 'com.android.volley:volley:1.0.0'

This problem comes when you use two libraries. To solve the problem, just remove

compile 'com.android.volley:volley:1.0.0' 

Your code will work fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM