空指针异常 anko for AndroidfindviewbyIdfragmentondestoryView

xiaoxiao2021-02-28  62

不废话直接上代码

HttpMethods.get().orderManage(object : Observer<HttpResult<OrderManageResult>> { override fun onNext(result: HttpResult<OrderManageResult>) { if (result.result == BaseActivity.SUCCESS) { Handler().postDelayed({ orderDatasLl.visibility = View.INVISIBLE }, 2000) val pageInfo = result.datas.pageInfo if (pageInfo != null) if (pageInfo.hasmore) iRecyclerView.setLoadMoreEnabled(true) else iRecyclerView.setLoadMoreEnabled(false) val order_list = result.datas.order_list returnListData(order_list) } else { if (result.result == BaseActivity.NODATAS) { orderListAdapter.clear() orderDatasLl.visibility = View.VISIBLE } iRecyclerView.setRefreshing(false) } }

这里用到的是retrofit+rxjava+anko+IRecyclerView 框架 基本是目前最流行的框架 Handler().postDelayed({ orderDatasLl.visibility = View.INVISIBLE }, 2000) 这里模拟延时操作 当orderDatasLl 为anko根据id查找到如果走的OndesToryView 方法会报anko特有的空指针异常 这也是困扰我很久的bug 秘制崩溃

改成 findViewbyId 就不会空指针异常 也不用一大堆判断 是否被销毁了 下拉和上拉动画也都会收回

lateinit var iRecyclerView: IRecyclerView lateinit var orderDatasLl: View orderDatasLl = rootView?.findViewById(R.id.order_datas_ll) as View iRecyclerView = rootView?.findViewById(R.id.irecyclerview) as IRecyclerView

新知识有新坑 这里做个笔记 也希望能解决遇到相同问题的人 当然也有很多其他的解法 目前就暂时这样吧

转载请注明原文地址: https://www.6miu.com/read-29806.html

最新回复(0)