'kotlin.UninitializedPropertyAccessException: lateinit property postFragment has not been initialized on FragmentStateAdapter Dagger Hilt
i tried to implement viewpager2 with tabLayout with dagger-hilt. then i got error when implementing viewpager2 on dagger hilt "kotlin.UninitializedPropertyAccessException: lateinit property postFragment has not been initialized". Here's my code
ProfilePagerAdapter
class ProfilesPagerAdapter(activity: AppCompatActivity): FragmentStateAdapter(activity) {
private lateinit var postFragment: PostFragment
override fun getItemCount(): Int = 3
override fun createFragment(position: Int): Fragment {
val fragment: Fragment? = null
when (position) {
0 -> postFragment
1 -> postFragment
2 -> postFragment
}
return fragment as Fragment
}
}
ProfileActivity
@AndroidEntryPoint
class ProfileActivity : AppCompatActivity() {
companion object {
@StringRes
private val TAB_TITLES = intArrayOf(
R.string.tab_text_post,
R.string.tab_text_review,
R.string.tab_text_koleksi
)
}
private lateinit var binding: ActivityProfileBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityProfileBinding.inflate(layoutInflater)
setContentView(binding.root)
supportActionBar?.hide()
val profilesPagerAdapter = ProfilesPagerAdapter(this)
binding.apply {
viewpagerProfile.adapter = profilesPagerAdapter
TabLayoutMediator(tabProfile, viewpagerProfile) { tab, position ->
tab.text = resources.getString(TAB_TITLES[position])
}.attach()
}
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
when (item.itemId) {
R.id.share -> {
Toast.makeText(this, "Fitur belum dibuat", Toast.LENGTH_SHORT).show()
return true
}
R.id.help -> {
Toast.makeText(this, "Fitur belum dibuat", Toast.LENGTH_SHORT).show()
return true
}
R.id.home_menu -> {
Toast.makeText(this, "Fitur belum dibuat", Toast.LENGTH_SHORT).show()
return true
}
else -> return true
}
}
}
A tutorial or reference about implementing viewpager2 using dagger-hilt is appreciated
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|