我的代码适用于视图模型,但是当我开始使用实时数据观察值时
model.d.observe(this, Observer<RespCategories> {
r->resp=r
})
r inresp=r
的类型从RespCategories
to更改为RespCategories?
与寻呼机适配器的参数不匹配,因此我对其进行了一些更改,例如转换 var resp: RespCategories
为, var resp: RespCategories? = null
因此在观察r->resp=r
resp 值为 null 时出现了一个新问题。如何解决问题?
kotlin.KotlinNullPointerException at com.example.xyz.MainActivity.showData(MainActivity.kt:83)
at com.example.xyz.MainActivity$fetchData$1.onResponse(MainActivity.kt:65)
主要活动
class MainActivity : AppCompatActivity()
{
var userService: UserService = ApiUtils.getUserService()
var dotscount: Int = 0
lateinit var sliderDotspanel: LinearLayout
lateinit var sectionPagerAdapter: SectionPagerAdapter
lateinit var respCategories: RespCategories
var resp: RespCategories? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
sliderDotspanel = findViewById(R.id.sliderdots)
fetchData()
}
fun fetchData() {
var call: Call<RespCategories>
call = userService.getData()
call.enqueue(object : Callback<RespCategories> {
override fun onFailure(call: Call<RespCategories>, t: Throwable) {
Toast.makeText(this@MainActivity, "fail", Toast.LENGTH_LONG).show()
}
override fun onResponse(call: Call<RespCategories>, response: Response<RespCategories>) {
Toast.makeText(this@MainActivity, "success", Toast.LENGTH_LONG).show()
respCategories = response.body()!!
var dataGenerator:DataGenerator
dataGenerator=DataGenerator()
dataGenerator.addData(respCategories)
// DataGenerator.addData(respCategories)
showData()
}
})
}
fun showData(){
//var resp:RespCategories=DataGenerator.getData()
val model=ViewModelProviders.of(this).get(DataGenerator::class.java)
// var resp=model.getData()
model.d.observe(this, Observer<RespCategories> {
r->resp=r
})
if (viewPager != null) {
sectionPagerAdapter = SectionPagerAdapter(supportFragmentManager, resp)
viewPager.adapter = sectionPagerAdapter
dotscount = sectionPagerAdapter.getCount()
var dots: Array<ImageView?>? = null
dots = arrayOfNulls(dotscount)
for (i in 0 until dotscount) {
dots[i] = ImageView(this@MainActivity)
dots[i]!!.setImageDrawable(ContextCompat.getDrawable(applicationContext, R.drawable.non_active_dots))
val params = LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT)
params.setMargins(8, 0, 8, 0)
sliderDotspanel.addView(dots[i], params)
}
dots[0]!!.setImageDrawable(ContextCompat.getDrawable(applicationContext, R.drawable.active_dots))
viewPager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {
}
override fun onPageSelected(position: Int) {
for (i in 0 until dotscount) {
dots[i]!!.setImageDrawable(ContextCompat.getDrawable(applicationContext, R.drawable.non_active_dots))
}
dots[position]!!.setImageDrawable(ContextCompat.getDrawable(applicationContext, R.drawable.active_dots))
}
override fun onPageScrollStateChanged(state: Int) {
}
})
}
}
}
>Datagenerator
````````````
class DataGenerator constructor(): ViewModel() {
companion object {
// lateinit var da:RespCategories
// lateinit var da:MutableLiveData<RespCategories>
}
val d=MutableLiveData<RespCategories>()
fun addData(respCategories: RespCategories){
// d=respCategories
d.value=respCategories
}
fun getData():MutableLiveData<RespCategories>{
return d
}
}
>RespCategories
````````````
data class RespCategories constructor(
@field:SerializedName("type")
val type: String? = null,
@field:SerializedName("value")
val value: List<String?>? = null
)
[1]: https://i.stack.imgur.com/eDAhx.png