匕首柄-碎片绑定为空?



更新

如果我从SearchFragment中的mPresenter中删除@Inject注释,并在运行时创建依赖关系,例如在onCreateView()中通过

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
mPresenter = SearchPresenter(this)
// ...
}

然后这个bug就神奇地消失了,所以它可能和Hilt有关。

原帖

我在我的应用程序中使用dagger柄2.38,它有一个"活动"和一个"片段",它又有一个"EditText"。然而,在' onTextChange() '事件后,我得到:

芬兰湾的科特林。UninitializedPropertyAccessException: lateinit属性未初始化mBindingSearchFragment.onSearchResult (SearchFragment.kt: 65)在SearchPresenter.search (SearchPresenter.kt: 18)在SearchFragment onCreateView 1.美元ontextchanged (SearchFragment.kt: 36)在…

任何帮助都非常感谢!下面是我的设置:

MainActivity

@AndroidEntryPoint
class MainActivity : AppCompatActivity() {
@Inject
lateinit var mSearchFragment: SearchFragment
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
supportFragmentManager
.beginTransaction()
.replace(R.id.main_fragment_container, mSearchFragment)
.commit()
}

}

搜索模块

@Module
@InstallIn(ActivityComponent::class)
object SearchModule {
@Provides
fun provideView(): SearchContract.View {
return SearchFragment()
}
@Provides
fun providePresenter(view: SearchContract.View): SearchContract.Presenter {
return SearchPresenter(view)
}
}

SearchFragment

class SearchFragment @Inject constructor() : Fragment(), SearchContract.View {
private lateinit var mBinding: FragmentSearchBinding
@Inject
lateinit var mPresenter: SearchContract.Presenter
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
mBinding = FragmentSearchBinding.inflate(inflater, container, false)
mBinding.edittextSearch.addTextChangedListener(object : TextWatcher {
override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
mPresenter.search(p0.toString().trim())
}
override fun afterTextChanged(p0: Editable?) {}
override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {}
})
mBinding.recyclerview.apply {
adapter = SearchAdapter(mPresenter.getCompanies())
layoutManager = LinearLayoutManager(this@SearchFragment.activity)
}
return mBinding.root
}
override fun onSearchResult(symbols: List<Company>) {
(mBinding.recyclerview.adapter as SearchAdapter).updateSymbols(symbols)
(mBinding.recyclerview.adapter as SearchAdapter).notifyDataSetChanged()
}
}

SearchPresenter

@ActivityScope
class SearchPresenter @Inject constructor(private val mView: SearchContract.View) : SearchContract.Presenter {
override fun search(filter: String) {
mView.onSearchResult(listOf(Company(0, "APPL", "Apple Inc.")))
}
}

我认为onSearchResultonCreateView之前被调用,所以你可以尝试在初始化中将mBinding设置为null并删除lateint,这样你就可以检查(mBinidng != null)是否做了你想要的

相关内容

  • 没有找到相关文章

最新更新