resp=r })-6ren">
gpt4 book ai didi

android - 如何在观察实时数据值的同时修复 "resp"的空指针

转载 作者:行者123 更新时间:2023-11-29 02:21:22 26 4
gpt4 key购买 nike

我的代码在 View 模型上运行良好,但是当我开始使用

中的实时数据观察值时
model.d.observe(this, Observer<RespCategories> {
r->resp=r
})

resp=r 中 r 的类型从 RespCategories 更改为 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)

MainActivity

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]: /image/eDAhx.png

最佳答案

您的代码中存在错误:您在 showData() 中的观察者观察到一个 DataGenerator 对象,但是 onResponse() 回调发布数据到另一个。你应该

关于 LiveData 的类型参数可空性的几句话。查看Observer界面:

public interface Observer<T> {
void onChanged(@Nullable T t);
}

onChanged()方法的参数用@Nullable注解。此方法根据 LiveData 类型参数的可空性观察可空类型。即使您的 LiveData 使用非 null 类型参数定义,您也可以期待 null:

  • 如果您在新对象上调用 liveData.value,但您可以覆盖默认空值并设置您自己的值

    val d = MutableLiveData<RespCategories>().apply { value = RespCategories.DEFAULT }
  • 如果您的 java 代码发布空值(不被禁止)

关于android - 如何在观察实时数据值的同时修复 "resp"的空指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55429465/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com