布局视图未使用Butterknife初始化

时间:2019-04-30 09:52:51

标签: kotlin butterknife

在以下代码中:

class LobbyFragment : Fragment() {

    @Inject
    lateinit var lobbyFragmentHelloService: LobbyFragmentHelloService

    @BindView(R.id.lobby_fragment_hello)
    lateinit var lobbyFragmentHelloTextView: TextView

    lateinit var unbinder: Unbinder

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        val view = inflater.inflate(R.layout.lobby_fragment, container, false)
        unbinder = ButterKnife.bind(this, view)
        return view
    }

    override fun onViewCreated(view: View?, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        sayFragmentHello()
    }

    override fun onAttach(context: Context?) {
        AndroidInjection.inject(this)
        super.onAttach(context)
    }

    override fun onDestroyView() {
        super.onDestroyView()
        unbinder.unbind()
    }

    private fun sayFragmentHello() {
        lobbyFragmentHelloTextView.text = lobbyFragmentHelloService.sayHello()
    }
}

lobbyFragmentHelloTextView从未初始化。 Butterknife用于初始化此变量。为什么在调用sayFramentHello时未进行初始化?

1 个答案:

答案 0 :(得分:2)

我不太确定出了什么问题,但是要解决此问题,您可以考虑使用内置于合成装订中的kotlin并去除黄油刀。效率更高。

explained here