2017-01-10 6 views
-1

Когда я запустить приложение его дать мне ошибки, и когда я обновить приложение его зависаниеAndroid Попытки вызвать виртуальный метод «...» на нулевой ссылке на объект

Моего код

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_refresh_layout); 
    swipeRefreshLayout.setOnRefreshListener(this); 

    city = (TextView)findViewById(R.id.city); 

    country = (TextView) findViewById(R.id.country); 
    update = (TextView) findViewById(R.id.last_update); 
    temp = (TextView) findViewById(R.id.temp); 
    state = (TextView) findViewById(R.id.state); 

    swipeRefreshLayout.post(new Runnable() { 
     @Override 
     public void run() { 
      swipeRefreshLayout.setRefreshing(true); 
      fetchWeather(); 
     } 
    }); 


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

      city.setText(response 
        .body() 
        .getQuery() 
        .getResults() 
        .getChannel() 
        .getLocation() 
        .getCity()); 

      country.setText(response 
        .body() 
        .getQuery() 
        .getResults() 
        .getChannel() 
        .getLocation() 
        .getCountry()); 
      update.setText(response 
        .body() 
        .getQuery() 
        .getResults() 
        .getChannel() 
        .getLastBuildDate()); 
      temp.setText(response.body().getQuery().getResults().getChannel().getItem().getCondition().getTemp() +"\u2109"); 
      state.setText(response.body().getQuery().getResults().getChannel().getLocation().getRegion()); 



      swipeRefreshLayout.setRefreshing(false); 

     } 

     @Override 
     public void onFailure(Call<Weather> call, Throwable t) { 
      Log.e("Failed",t.getMessage()); 
      swipeRefreshLayout.setRefreshing(false); 
     } 
    }); 
} 


@Override 
public void onRefresh() { 
    fetchWeather(); 
} 

} класс

Модель Результат

public class Results { 

@SerializedName("channel") 
@Expose 
private Channel channel; 



public void setChannel(Channel channel) { 
    this.channel = channel; 
} 

public Channel getChannel() { 
    return channel; 
} 

}

модель класса канала

public class Channel { 

@SerializedName("units") 
@Expose 
private Units units; 
@SerializedName("title") 
@Expose 
private String title; 
@SerializedName("link") 
@Expose 
private String link; 
@SerializedName("description") 
@Expose 
private String description; 
@SerializedName("language") 
@Expose 
private String language; 
@SerializedName("lastBuildDate") 
@Expose 
private String lastBuildDate; 
@SerializedName("ttl") 
@Expose 
private String ttl; 
@SerializedName("location") 
@Expose 
private Location location; 
@SerializedName("wind") 
@Expose 
private Wind wind; 
@SerializedName("atmosphere") 
@Expose 
private Atmosphere atmosphere; 
@SerializedName("astronomy") 
@Expose 
private Astronomy astronomy; 
@SerializedName("image") 
@Expose 
private Image image; 
@SerializedName("item") 
@Expose 
private Item item; 

public Units getUnits() { 
    return units; 
} 

public void setUnits(Units units) { 
    this.units = units; 
} 

public String getTitle() { 
    return title; 
} 

public void setTitle(String title) { 
    this.title = title; 
} 

public String getLink() { 
    return link; 
} 

public void setLink(String link) { 
    this.link = link; 
} 

public String getDescription() { 
    return description; 
} 

public void setDescription(String description) { 
    this.description = description; 
} 

public String getLanguage() { 
    return language; 
} 

public void setLanguage(String language) { 
    this.language = language; 
} 

public String getLastBuildDate() { 
    return lastBuildDate; 
} 

public void setLastBuildDate(String lastBuildDate) { 
    this.lastBuildDate = lastBuildDate; 
} 

public String getTtl() { 
    return ttl; 
} 

public void setTtl(String ttl) { 
    this.ttl = ttl; 
} 

public Location getLocation() { 
    return location; 
} 

public void setLocation(Location location) { 
    this.location = location; 
} 

public Wind getWind() { 
    return wind; 
} 

public void setWind(Wind wind) { 
    this.wind = wind; 
} 

public Atmosphere getAtmosphere() { 
    return atmosphere; 
} 

public void setAtmosphere(Atmosphere atmosphere) { 
    this.atmosphere = atmosphere; 
} 

public Astronomy getAstronomy() { 
    return astronomy; 
} 

public void setAstronomy(Astronomy astronomy) { 
    this.astronomy = astronomy; 
} 

public Image getImage() { 
    return image; 
} 

public void setImage(Image image) { 
    this.image = image; 
} 

public Item getItem() { 
    return item; 
} 

public void setItem(Item item) { 
    this.item = item; 
} 

}

файл манифеста

<uses-permission android:name="android.permission.INTERNET"/> 
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> 
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/> 

Ошибка приложения работает в Wi-Fi. Когда я бегу Мобильный Интернет не дает ошибки ......

FATAL EXCEPTION: main 
                     Process: com.example.prem.climate, PID: 11893 
                     java.lang.NullPointerException: Attempt to invoke virtual method 'com.example.prem.climate.model.Channel com.example.prem.climate.model.Results.getChannel()' on a null object reference 
                      at com.example.prem.climate.MainActivity$2.onResponse(MainActivity.java:68) 
                      at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.run(ExecutorCallAdapterFactory.java:68) 
                      at android.os.Handler.handleCallback(Handler.java:746) 
                      at android.os.Handler.dispatchMessage(Handler.java:95) 
                      at android.os.Looper.loop(Looper.java:148) 
                      at android.app.ActivityThread.main(ActivityThread.java:5443) 
                      at java.lang.reflect.Method.invoke(Native Method) 
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728) 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618 

ответ

0

Это похоже response.body().getQuery().getResults() возвращается null, а затем вы пытаетесь вызвать getChannel() на null

+0

ли вы разработать эту вещь ?. Я не получил вашу точку ясно – prem

+1

Посмотрите на этот код: city.setText (ответ .Body() .getQuery() .getResults() .getChannel() .getLocation() .getCity()); В цепочке методов вы должны выяснить, почему getResults() возвращает null. Попытайтесь проверить ответ, если у него есть тело, и нет ошибок http – Dmitriy

+0

Все данные являются ответом от сервера, который выглядит хорошо ... Я запускаю это приложение в мобильном Интернете, он не дает никакой ошибки, но когда я запускаю в Wifi интернет, дайте мне ошибку – prem

Смежные вопросы

 Смежные вопросы