Moxy为什么不初始化Presenter(Kotlin,Android)

时间:2019-04-05 16:18:55

标签: android kotlin

我收到mPresenter尚未初始化的错误。我不明白为什么?
使用Kotlin时出现此错误,但是如果使用JAVA,一切都很好
这是我的代码

查看

@StateStrategyType(value = AddToEndStrategy::class)
interface IHelloWorldView : MvpView {
    fun showMessage(message: Int)
}

演示者

@InjectViewState
class HelloWorldPresenter : MvpPresenter<IHelloWorldView>() {
    fun show() = viewState.showMessage(R.string.message)
}

MainActivity

class MainActivity : MvpAppCompatActivity(), IHelloWorldView {

    @InjectPresenter
    lateinit var mPresenter: HelloWorldPresenter

    lateinit var mText: TextView
    lateinit var mButton: Button

    override fun showMessage(message: Int) {
        mText.setText(message)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        mText = findViewById(R.id.text)
        mButton = findViewById(R.id.button)
        mButton.setOnClickListener {
            mPresenter.show()
        }
    }
}

2 个答案:

答案 0 :(得分:0)

因为您需要像在JAVA Foo foo = new Foo();中那样进行初始化

似乎您只创建了一个变量

class MainActivity : MvpAppCompatActivity(), IHelloWorldView {

    @InjectPresenter
    lateinit var mPresenter: HelloWorldPresenter
    //initialisation

    @ProvidePresenterTag(presenterClass = HelloWorldPresenter::class, type =PresenterType.GLOBAL)
    fun providemPresenter() = HelloWorldPresenter()
    fun mPresenter() = HelloWorldPresenter()

    lateinit var mText: TextView
    lateinit var mButton: Button

    override fun showMessage(message: Int) {
        mText.setText(message)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        mText = findViewById(R.id.text)
        mButton = findViewById(R.id.button)
        mButton.setOnClickListener {
            mPresenter.show()
        }
    }
}

答案 1 :(得分:-1)

类型= PresenterType.GLOBAL -不存在(至少在kotlin中)