变量中的变量值为null

时间:2017-12-30 04:58:05

标签: android kotlin retrofit2

我有一个有适配器的片段。我正在使用改造来从网络上获取数据

我的支持

class MyTransactionFragment : Fragment() {
    val disposable = CompositeDisposable()
    var transactionDetailList:ArrayList<TransactionData>? = null
    private var mApiSocket: ApiSocket? = null
    private var mRecyclerView:RecyclerView? = null
    var adapter:TransactionAdapter? = null
    companion object {
        val TAG = ItemFragment::class.java.simpleName!!
        fun newInstance(transactionPojo: TransactionPojo): MyTransactionFragment {
            val fragment = MyTransactionFragment()
            val args = Bundle()
            args.putSerializable(Constants.TRANSACTIONS, transactionPojo)
            fragment.arguments = args
            return fragment
        }
    }
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {
        val view = inflater.inflate(R.layout.fragment_my_transaction, container, false)
        mApiSocket = Utils().getApiSocket(activity!!)
        val layoutManager = LinearLayoutManager(activity, LinearLayoutManager.VERTICAL, false)
        view.mRecyclerViewTransaction.layoutManager = layoutManager
        view.mRecyclerViewTransaction.setHasFixedSize(true)
        if (arguments!=null && arguments!!.getSerializable(Constants.TRANSACTIONS) != null){
            val transactionPojo = arguments!!.getSerializable(Constants.TRANSACTIONS) as TransactionPojo
            transactionDetailList = ArrayList(transactionPojo.transactions.data)
            adapter = TransactionAdapter(transactionDetailList, activity!!,disposable)
            view.mRecyclerViewTransaction.adapter = adapter
            mRecyclerView = view.mRecyclerViewTransaction
            val scrollListener = object : EndlessRecyclerViewScrollListener(layoutManager) {
                override fun onLoadMore(page: Int, totalItemsCount: Int, view: RecyclerView) {
                    disposable.add(mApiSocket!!.transaction(""+page+1)
                            .subscribeOn(Schedulers.newThread())
                            .observeOn(AndroidSchedulers.mainThread())
                            .subscribe(MyTransactionFragment()::onResponse,MyTransactionFragment()::onError))
                }
            }
            view.mRecyclerViewTransaction.addOnScrollListener(scrollListener)
        }
        return view
    }

    private fun onResponse(transactionPojo: TransactionPojo){
        transactionDetailList = ArrayList(transactionPojo.transactions.data)
        adapter!!.addData(transactionDetailList)  //here adapter variable gets null Why
    }

    fun onError(e: Throwable) {
        Log.e("TransactionFragment", e.toString())
        if (e is NoNetWorkException) {
            Utils().showSnackBar(activity!!, "No Network connection")
        } else
            Utils().showSnackBar(activity!!, "Something went wrong please try again later")
        Utils().hideProgress()
    }

    override fun onDetach() {
        super.onDetach()
        disposable.dispose()
    }

}

在onResponse中的此适配器变量中为空?为什么?

1 个答案:

答案 0 :(得分:0)

最后我发现了

实际上我在订阅者中呼叫MyTransactionFragment()::onResponse。这意味着我将结果放在MyTransactionFragment的新实例上。所以我改成了

disposable.add(mApiSocket!!.transaction(""+page+1)
                            .subscribeOn(Schedulers.newThread())
                            .observeOn(AndroidSchedulers.mainThread())
                            .subscribe(::onResponse,::onError))