方法调用产生NPE - Retrofit

时间:2017-05-19 06:54:51

标签: java android retrofit retrofit2

单击浮动操作按钮时,我正试图在我的应用程序中设置NavHeader的天气。

fab.setOnClickListener(new View.OnClickListener() {
      @Override public void onClick(View view) {

        WeatherAPI.Factory.getInstance().getWeather().enqueue(new Callback<Weather>() {
          @Override public void onResponse(Call<Weather> call, Response<Weather> response) {

            Query query = response.body().getQuery();
            degree.setText(query.getResults().getChannel().getItem().getCondition().getTemp());
          }

          @Override public void onFailure(Call<Weather> call, Throwable t) {

            Log.e("failure", t.getMessage());
          }
        });
      }
    });

但是当我点击按钮时,我得到了NPE。

public interface WeatherAPI {

  String BASE_URL = "https://query.yahooapis.com/v1/public/";

  @GET("PATH CONTINUES HERE")
  Call<Weather> getWeather();

  class Factory {

    private static WeatherAPI service;

    public static WeatherAPI getInstance() {

      if (service == null) {

        Retrofit retrofit =
            new Retrofit.Builder().addConverterFactory(GsonConverterFactory.create())
                .baseUrl(BASE_URL)
                .build();

        retrofit.create(WeatherAPI.class);

        return service;
      } else {
        return service;
      }
    }
  }
}

它警告getQuery()调用可能会产生NullPointerException。

这是我的Query类:

public class Query {

    @SerializedName("count")
    @Expose
    private int count;
    @SerializedName("created")
    @Expose
    private String created;
    @SerializedName("lang")
    @Expose
    private String lang;
    @SerializedName("results")
    @Expose
    private Results results;

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    public String getCreated() {
        return created;
    }

    public void setCreated(String created) {
        this.created = created;
    }

    public String getLang() {
        return lang;
    }

    public void setLang(String lang) {
        this.lang = lang;
    }

    public Results getResults() {
        return results;
    }

    public void setResults(Results results) {
        this.results = results;
    }

}

这是错误:

  

05-19 09:46:22.399 26168-26168 / us.egek.rockclimbingkaynaklar   E / AndroidRuntime:致命异常:主要                                                                                  过程:us.egek.rockclimbingkaynaklar,PID:26168                                                                                  java.lang.RuntimeException:无法启动活动   ComponentInfo {us.egek.rockclimbingkaynaklar / us.egek.rockclimbingkaynaklar.Activities.MainActivity}:   显示java.lang.NullPointerException                                                                                      在   android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2318)                                                                                      在   android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2396)                                                                                      在android.app.ActivityThread.access $ 800(ActivityThread.java:139)                                                                                      在   android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1293)                                                                                      在android.os.Handler.dispatchMessage(Handler.java:102)                                                                                      在android.os.Looper.loop(Looper.java:149)                                                                                      在android.app.ActivityThread.main(ActivityThread.java:5257)                                                                                      at java.lang.reflect.Method.invokeNative(Native Method)                                                                                      在java.lang.reflect.Method.invoke(Method.java:515)                                                                                      在   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:793)                                                                                      在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)                                                                                      at dalvik.system.NativeStart.main(Native Method)                                                                                   引起:java.lang.NullPointerException                                                                                      在   us.egek.rockclimbingkaynaklar.Activities.MainActivity.onCreate(MainActivity.java:93)

1 个答案:

答案 0 :(得分:2)

WeatherAPI.Factory.getInstance()方法中,您应该将服务属性分配给改造创建语句

service = retrofit.create(WeatherAPI.class);