gpt4 book ai didi

android - 避免Kotlin中的竞争条件,Smartcast是不可能的运行时异常

转载 作者:行者123 更新时间:2023-12-02 13:22:35 24 4
gpt4 key购买 nike

该行:

viewAdapter.setTitleData(bezinningModels)



下面的代码段给了我运行时警告:

Smart cast to 'BezinningAdapter' is impossible, because 'viewAdapter' is a mutable property that could have been changed by this time



我浏览了很多带有类似运行时警告的帖子,并且我理解了为什么会发生这种情况。基本上Kotlin想要避免竞争条件,以使可变对象的值不会改变,但是我仍然没有得到应避免的错误,因此我可以将troitfit调用调用到 RecyclerView Array
我试图将 var的值从 val更改为 RecyclerView,但没有用

以下是导致问题的代码段:
@RequiresApi(Build.VERSION_CODES.HONEYCOMB)
class MainFragment : android.app.Fragment() {




private lateinit var viewManager: RecyclerView.LayoutManager
private lateinit var recyclerView: RecyclerView

private lateinit var viewAdapter: RecyclerView.Adapter<*>

override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? {

val view = inflater!!.inflate(R.layout.bezinning_fragment, container, false)

viewManager=LinearLayoutManager(activity)
viewAdapter=BezinningAdapter()

recyclerView = view.findViewById(R.id.recycler_view) as RecyclerView
recyclerView.setHasFixedSize(true)
recyclerView.layoutManager=viewManager
recyclerView.adapter=viewAdapter


val subscribe = bezinningListViewModel!!.showBezinningLijst("132")
?.subscribeOn(schedulerProvider!!.io())
?.observeOn(schedulerProvider!!.ui())
?.subscribe(object : FlowableSubscriber<List<BezinningModel>> {
override fun onError(t: Throwable?) {
Log.d("IVO", "onError: ")
}

override fun onComplete() {
Log.d("IVO", "onComplete: ")
}

override fun onSubscribe(s: Subscription) {
s.request(Long.MAX_VALUE);

}

override fun onNext(bezinningModels: List<BezinningModel>?)
{

val JSONResponseBody = bezinningModels!!
[0].attribute.body
viewAdapter.setTitleData(bezinningModels)


val bodyBezinningParsedHtml =
Html.fromHtml(JSONResponseBody)



}


})

return view

}



}

最佳答案

如果您知道viewAdapter将是BezinningAdapter,则应这样声明:

private lateinit var viewAdapter: BezinningAdapter<*>

关于android - 避免Kotlin中的竞争条件,Smartcast是不可能的运行时异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51934492/

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