未经检查的方法调用作为原始类型的成员
Unchecked call to method as a member of raw type
我的项目中显示以下警告 -
未检查调用 'getWeatherData(T,Boolean,String)' 作为原始类型 'IWeatherCallbackListener' 的成员。
我创建了以下界面 -
public interface IWeatherCallbackListener<T> {
void getWeatherData(T weatherModel, Boolean success, String errorMsg);
}
并按以下方式调用它,
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());
}
});
}
我已经在我的 MainActivity 中实现了这个接口并将该方法称为 -
WeatherConditions.getOpenWeatherData(etCityName.getText().toString(), OPEN_WEATHER_APP_ID, MainActivity.this)
谁能帮忙解释一下这个警告。
看起来你也必须声明你的 T
类型,在你的情况下它必须是 class 的 response.body()
实例。
尝试替换行
public static void getOpenWeatherData(String city, String appId, IWeatherCallbackListener listener)
到
public static void getOpenWeatherData(String city, String appId, IWeatherCallbackListener<ResponseBody> listener)
之所以如此,是因为当您声明接口时
IWeatherCallbackListener<T>
您使用 T 及其原始类型。创建实例时,您必须显示 确切 您将使用的类型或您希望接收的确切类型作为参数。
例如,如果您手动创建必须如下所示的侦听器
IWeatherCallbackListener<ResponseBody> listener = new IWeatherCallbackListener<ResponseBody>() {
//implementation of methods
}
参数也一样,你必须展示你能收到什么T
。
我的项目中显示以下警告 -
未检查调用 'getWeatherData(T,Boolean,String)' 作为原始类型 'IWeatherCallbackListener' 的成员。
我创建了以下界面 -
public interface IWeatherCallbackListener<T> {
void getWeatherData(T weatherModel, Boolean success, String errorMsg);
}
并按以下方式调用它,
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());
}
});
}
我已经在我的 MainActivity 中实现了这个接口并将该方法称为 -
WeatherConditions.getOpenWeatherData(etCityName.getText().toString(), OPEN_WEATHER_APP_ID, MainActivity.this)
谁能帮忙解释一下这个警告。
看起来你也必须声明你的 T
类型,在你的情况下它必须是 class 的 response.body()
实例。
尝试替换行
public static void getOpenWeatherData(String city, String appId, IWeatherCallbackListener listener)
到
public static void getOpenWeatherData(String city, String appId, IWeatherCallbackListener<ResponseBody> listener)
之所以如此,是因为当您声明接口时
IWeatherCallbackListener<T>
您使用 T 及其原始类型。创建实例时,您必须显示 确切 您将使用的类型或您希望接收的确切类型作为参数。
例如,如果您手动创建必须如下所示的侦听器
IWeatherCallbackListener<ResponseBody> listener = new IWeatherCallbackListener<ResponseBody>() {
//implementation of methods
}
参数也一样,你必须展示你能收到什么T
。