【已解决】 彩云天气无法成功获取天气信息(按照《第一行代码》第3版书上的代码编写的)
学习《第一行代码》第3版敲书上的彩云天气出现了错误,已经完成了全国地区的显示和天气的显示,但是天气的显示有问题,点击相关地方以后无法成功获取天气信息。
package com.weiwei.sunnyweather.ui.weather
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import android.widget.Toast
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.get
import com.weiwei.sunnyweather.R
import com.weiwei.sunnyweather.logic.model.Weather
import com.weiwei.sunnyweather.logic.model.getSky
import com.weiwei.sunnyweather.ui.place.PlaceViewModel
import kotlinx.android.synthetic.main.activity_weather.*
import kotlinx.android.synthetic.main.forecast.*
import kotlinx.android.synthetic.main.life_index.*
import kotlinx.android.synthetic.main.now.*
import java.text.SimpleDateFormat
import java.util.*
class WeatherActivity : AppCompatActivity() {
val viewModel by lazy { ViewModelProvider(this).get(WeatherViewModel::class.java) }
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_weather)
if(viewModel.locationLng.isEmpty()){
viewModel.locationLng = intent.getStringExtra("location_lng")?:""
}
if(viewModel.locationLat.isEmpty()){
viewModel.locationLat = intent.getStringExtra("location_lat")?:""
}
if(viewModel.placeName.isEmpty()){
viewModel.placeName = intent.getStringExtra("place_name")?:""
}
viewModel.weatherLiveData.observe(this,Observer{ result->
val weather = result.getOrNull()
if(weather!=null){
showWeatherInfo(weather)
}else{
Toast.makeText(this,"无法成功获取天气信息",Toast.LENGTH_LONG).show()
result.exceptionOrNull()?.printStackTrace()
}
})
viewModel.refreshWeather(viewModel.locationLng,viewModel.locationLat)
}
......
}
下图是点击RecycerView中的item报错

已经完成了全国地区的显示和天气的显示,但是天气的显示有问题
所以是完成了吗
日志好像没有啥
1.判断接口是否有数据 (查询城市无效估计是这个接口的问题了 postman看看)
2.判断是否显示的rc是否步骤写全了
3.有图贴图 有报错贴日志
这个日志,也没发现什么问题,是不是你的点击,没有实现