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 时没有初始化?
解决方案
我不太确定出了什么问题,但要解决这个问题,您可以考虑使用内置在合成绑定中的 kotlin 并摆脱黄油刀。它更有效率。
推荐阅读
- go - float64 的值可以支持多小?
- windows-installer - 如何确保任何包含 .ISM 项目中添加的 DLL 的新组件在生成 MSI 时始终获取文件表中的最后一个序列号
- google-app-engine - app-gradle-plugin 在多项目构建中中断任务
- c++ - wxwidgets 和 VSCode 的问题
- c# - 使用 ASP.NET MVC 和 C# 启用具有多个代码的同一用户
- r - 如何解决错误:`filter()` 输入 `..1` 的问题与函数之间的错误?
- c++ - 在抛出 'std::out_of_range' 的实例后调用终止,我不知道如何解决它
- python-3.x - 有没有更好的方法在列表 python 中循环和插入元素
- google-cloud-firestore - Firestore 添加关系数据?
- python - 如何指定适用于 Windows 的文件路径?