【问题标题】:Unchecked call to method as a member of raw type作为原始类型的成员对方法的未经检查的调用
【发布时间】:2018-04-13 11:29:45
【问题描述】:

以下警告显示在我的项目中 -

未经检查地调用“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)

谁能帮忙解释一下这个警告。

【问题讨论】:

    标签: java android generics interface generic-interface


    【解决方案1】:

    看起来您也必须声明您的 T 类型,在您的情况下,它必须是 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你能收到什么。

    【讨论】:

    • 感谢@DEADMC,您的解决方案运行良好,但请您解释一下,因为我不明白这个概念。这将是一个很大的帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-17
    • 1970-01-01
    • 1970-01-01
    • 2016-10-22
    • 2021-04-17
    相关资源
    最近更新 更多