Unchecked call to method as a member of raw type
Asked Answered
R

1

11

Following warning is shown in my project -

Unchecked call to 'getWeatherData(T,Boolean,String)' as a member of raw type 'IWeatherCallbackListener'.

I have created following interface -

public interface IWeatherCallbackListener<T> {
 void getWeatherData(T weatherModel, Boolean success, String errorMsg);
}

And called it in following way,

public class WeatherConditions {

private static IWeatherApi mWeatherApi;

/**
 * @param city
 * @param appId
 * @param listener
 */
public static void getOpenWeatherData(String city, String appId, IWeatherCallbackListener listener) {

    mWeatherApi = ApiService.getRetrofitInstance(BASE_URL_OPEN_WEATHER).create(IWeatherApi.class);
    Call<OpenWeatherModel> resForgotPasswordCall = mWeatherApi.getOpenWeatherData(appId, city);
    resForgotPasswordCall.enqueue(new Callback<OpenWeatherModel>() {
        @Override
        public void onResponse(Call<OpenWeatherModel> call, Response<OpenWeatherModel> response) {
            if (response.body() != null) {
                if (listener != null)
                    listener.getWeatherData(response.body(), true, "");
            }
        }

        @Override
        public void onFailure(Call<OpenWeatherModel> call, Throwable t) {
            if (listener != null)
                 listener.getWeatherData(null, false, t.getMessage());
        }
    });
}

I have implemented this interface in my MainActivity and called the method as -

WeatherConditions.getOpenWeatherData(etCityName.getText().toString(), OPEN_WEATHER_APP_ID, MainActivity.this)

Can anyone please help and explain this warning.

Ruelu answered 1/11, 2017 at 7:14 Comment(0)
A
10

Looks like you have to declare your T type too, in your case it has to be a class of response.body() instance.

Try to replace line

public static void getOpenWeatherData(String city, String appId, IWeatherCallbackListener listener)

to

public static void getOpenWeatherData(String city, String appId, IWeatherCallbackListener<ResponseBody> listener)

That is so because when you declare your interface

IWeatherCallbackListener<T>

You use T and its raw type. When you create instance you have to show what exactly type you will use or what exactly type you want to receive as argument.

For example if you create that listener manually that had to look like this

IWeatherCallbackListener<ResponseBody> listener = new IWeatherCallbackListener<ResponseBody>() {
    //implementation of methods
}

Same thing for arguments, you have to show what T you can receive.

Aurthur answered 1/11, 2017 at 7:25 Comment(1)
thanks @DEADMC your solution is working perfectly fine, but can you please explain it because I did not understand this concept.It would be a great help.Ruelu

© 2022 - 2024 — McMap. All rights reserved.