lateinit属性fragmentDispatchingAndroidInjector尚未初始化

时间:2018-02-20 07:09:05

标签: android kotlin

当我尝试更改显示的方向时 它会显示此错误“lateinit属性fragmentDispatchingAndroidInjector尚未初始化”

MainActivity.kt

class MainActivity : BaseActivity(), HasSupportFragmentInjector {

    @Inject
    internal lateinit var fragmentDispatchingAndroidInjector: DispatchingAndroidInjector<Fragment>
    internal lateinit var mainMenuPagerAdapter: MainMenuPagerAdapter


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        mainMenuPagerAdapter = MainMenuPagerAdapter(supportFragmentManager)
        setUpMainMenuPagerAdapter()
    }

    override fun onFragmentAttached() {
    }

    override fun onFragmentDetached(tag: String) {
    }

    override fun supportFragmentInjector(): AndroidInjector<Fragment>? {
        return fragmentDispatchingAndroidInjector
    }

    private fun setUpMainMenuPagerAdapter() {
        mainMenuPagerAdapter.count = 1
        mainMenuViewPager.adapter = mainMenuPagerAdapter
        tabLayout.addTab(tabLayout.newTab().setText(R.string.my_account).setIcon(R.drawable.ic_person_white_24dp))
        mainMenuViewPager.offscreenPageLimit = tabLayout.tabCount;
        mainMenuViewPager.addOnPageChangeListener(TabLayout.TabLayoutOnPageChangeListener(tabLayout));
        tabLayout.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
            override fun onTabSelected(tab: TabLayout.Tab) {
                mainMenuViewPager.currentItem = tab.position
            }
            override fun onTabUnselected(tab: TabLayout.Tab) {}
            override fun onTabReselected(tab: TabLayout.Tab) {}
        })
    }

}

2 个答案:

答案 0 :(得分:0)

旋转屏幕时发生了相同的崩溃,我在super.onCreate(savedInstanceState)之后调用了AndroidInjection.inject(this)。

override fun onCreate(savedInstanceState: Bundle?) {
        AndroidInjection.inject(this)
        super.onCreate(savedInstanceState)
       ...
    }

此后没有崩溃。可能会有帮助。

答案 1 :(得分:-1)

abstract class BaseActivity : AppCompatActivity(), MVPView, BaseFragment.CallBack {

    private var progressDialog: ProgressDialog? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        performDI()
    }

    override fun hideProgress() {
        progressDialog?.let { if (it.isShowing) it.cancel() }
    }

    override fun showProgress() {
        hideProgress()
        progressDialog = CommonUtil.showLoadingDialog(this)
    }

    private fun performDI() = AndroidInjection.inject(this)

}

这是我的BaseActivity.ky