java 作为原始类型的成员未经检查地调用方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47049917/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 09:29:10  来源:igfitidea点击:

Unchecked call to method as a member of raw type

javaandroidgenericsinterfacegeneric-interface

提问by Akash Bisariya

Following warning is shown in my project -

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

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

未经检查地调用“getWeatherData(T,Boolean,String)”作为原始类型“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 -

我已经在我的 MainActivity 中实现了这个接口,并将该方法称为 -

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

Can anyone please help and explain this warning.

任何人都可以请帮助并解释此警告。

回答by Andrey Danilov

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

看起来你也必须声明你的T类型,在你的情况下它必须是一个response.body()实例类。

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 exactlytype you will use or what exactly type you want to receive as argument.

您使用 T 及其原始类型。当您创建实例,你必须表现出什么确切类型,你会使用或究竟键入您希望收到作为参数。

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 Tyou can receive.

论点也是一样,你必须展示T你能得到什么。