布局视图未使用 Butterknife 初始化
Layout view is not initialized with 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 时没有初始化?
我不太确定出了什么问题,但要解决这个问题,您可以考虑使用内置合成绑定的 kotlin 并摆脱 butterknife。它更有效率。
explained here
在下面的代码中:
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 时没有初始化?
我不太确定出了什么问题,但要解决这个问题,您可以考虑使用内置合成绑定的 kotlin 并摆脱 butterknife。它更有效率。
explained here