如何在Android中将Dagger与MVP结合使用

时间:2019-03-29 18:38:48

标签: android kotlin nullpointerexception dagger-2 dagger

我想使用 Rxjava Dagger Kotin MVP 开发一个应用程序。
我编写了以下代码,但是当运行应用程序时,显示nullPointerExecetpion错误。
我知道nullPointerExeception是我的代码错误,但我设法发现了我的错误!

我的基本片段代码:

abstract class BaseFragment : Fragment(), BaseView {

    var presenter: BasePresenter<*>? = null

    abstract fun initializeDagger()
    abstract fun initializePresenter()
    abstract var layoutID: Int

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

        val view = inflater.inflate(layoutID, container, false)

        initializeDagger()
        initializePresenter()

        return view

    }

    override fun onDetach() {
        super.onDetach()
        presenter?.onDestroy()
    }
}

家庭演示者代码:

class HomeTodayPresenter @Inject constructor(
    val repositoryUseCase: RepositoryUseCase, disposable: CompositeDisposable
) : BasePresenter<HomeTodayView>(disposable) {

    private var todayList = ArrayList<Today>()

    fun onCreate() {
        view?.initRepositoryList(todayList)
        getTodayList()
    }

    fun onTodayRefreshList() {
        getTodayList()
    }

    private fun getTodayList() {
        compositeDisposable.add(
            repositoryUseCase.getAuctionsToday()
                .subscribe({ responseResult ->
                    view?.hideLoader()
                    responseResult?.let { itResponse ->
                        itResponse.res?.let { itRes ->
                            itRes.today?.let { itToday ->
                                if (itToday.size > 0) {
                                    todayList.clear()
                                    todayList.addAll(itToday)
                                    view?.loadRepositoryList()
                                }
                            }
                        }

                    }
                }, { e ->
                    view?.let { itView ->
                        e.message?.let { itErr ->
                            itView.showErrorMessage(itErr)
                        }
                        itView.hideLoader()
                    }
                })
        )
    }
}

首页片段代码:

class HomeTodayFragment : BaseFragment(), HomeTodayView {

    @Inject
    lateinit var homeTodayPresenter: HomeTodayPresenter
    lateinit var todayAuctionsAdapter: TodayAuctionsAdapter
    lateinit var layoutManager: LinearLayoutManager

    private val swipeRefreshListener = SwipeRefreshLayout.OnRefreshListener {
        homeTodayPresenter.onTodayRefreshList()
    }

    override fun initRepositoryList(list: ArrayList<Today>) {
        layoutManager = LinearLayoutManager(requireContext())
        todayAuctionsAdapter = TodayAuctionsAdapter(list)
        requireContext().initRecyclerView(homeFragToday_list, layoutManager, todayAuctionsAdapter)
    }

    override fun loadRepositoryList() {
        todayAuctionsAdapter.notifyDataSetChanged()
    }

    override fun hideLoader() {
        homeFragToday_loader.visibility = View.GONE
    }

    override fun showErrorMessage(msg: String) {
        Log.e("responseErr", msg)
    }

    override fun initializeDagger() {
        AndroidInjection.inject(requireActivity())
    }

    override fun initializePresenter() {
        super.presenter = homeTodayPresenter
        homeTodayPresenter.view = this
    }

    override var layoutID: Int = R.layout.fragment_home_today

    private lateinit var toolbarTile: TextView
    lateinit var handler: Handler


    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        homeTodayPresenter.onCreate()
        return super.onCreateView(inflater, container, savedInstanceState)

    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        //Initialize
        activity?.let {
            toolbarTile = it.findViewById(R.id.homePage_toolbarTitle)
        }
    }

    override fun setUserVisibleHint(isVisibleToUser: Boolean) {
        super.setUserVisibleHint(isVisibleToUser)
        if (isVisibleToUser) {
            //Initialize
            handler = Handler()
            //Set delay
            handler.postDelayed({
                //Set title
                toolbarTile.text = resources.getString(R.string.today)
                toolbarTile.setTextColor(ResourcesCompat.getColor(resources, R.color.green_active, null))
            }, 10)
        }
    }
}

LogCat错误:

kotlin.UninitializedPropertyAccessException: lateinit property homeTodayPresenter has not been initialized
    at com.app.applisttestapp.UI.Home.Fragments.Today.HomeTodayFragment.onCreateView(HomeTodayFragment.kt:69)
    at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2539)
    at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:875)
    at androidx.fragment.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManagerImpl.java:1227)
    at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:1293)

为此行显示错误:homeTodayPresenter.onCreate()

我该如何解决?

0 个答案:

没有答案