I use fragments with navigation in my app. I have the mainFragment class and the adminPwdFragment class. I want to go from the mainFragment to the adminPwdFragment with the press of a button on the interface. But I always get this error when tapping the button :
2020-05-16 17:12:53.302 1423-1423/com.example.lejosremote E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.lejosremote, PID: 1423 java.lang.ClassCastException: com.example.lejosremote.databinding.MainFragmentBindingImpl cannot be cast to com.example.lejosremote.databinding.AdminPwdFragmentBinding at com.example.lejosremote.admin_pwd.onCreateView(admin_pwd.kt:20) at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2698) at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:310) at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1185) at androidx.fragment.app.FragmentManager.addAddedFragments(FragmentManager.java:2222) at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:1995) at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:1951) at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:1847) at androidx.fragment.app.FragmentManager$4.run(FragmentManager.java:413) at android.os.Handler.handleCallback(Handler.java:790) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:164) at android.app.ActivityThread.main(ActivityThread.java:6494) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Here are the classes
class MainFragment : Fragment() {
private lateinit var viewModel: MainViewModel
private lateinit var binding: MainFragmentBinding
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View {
// Inflate view and obtain an instance of the binding class
binding = DataBindingUtil.inflate(
inflater,
R.layout.main_fragment,
container,
false
)
// ligne pour créer le ViewModel une seule fois
viewModel = ViewModelProviders.of(this).get(MainViewModel::class.java)
binding.mainViewModel = viewModel
binding.lifecycleOwner = this
viewModel.eventAdmin.observe(viewLifecycleOwner, Observer { admin ->
if (admin) {
findNavController().navigate(MainFragmentDirections.actionMainFragmentToAdminPwd())
}
})
return binding.root
}
admin_pwd is for the other fragment:
class admin_pwd : Fragment() {
private lateinit var viewModel: AdminPwdViewModel
private lateinit var viewModelFactory: AdminPwdViewModelFactory
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
val binding: AdminPwdFragmentBinding = DataBindingUtil.inflate(
inflater,
R.layout.main_fragment,
container,
false
)
viewModelFactory = AdminPwdViewModelFactory()
viewModel = ViewModelProviders.of(this).get(AdminPwdViewModel::class.java)
binding.mainViewModel = viewModel
binding.lifecycleOwner = this
return binding.root
}
What am I doing wrong ? How can I get rid of this error and make the navigation happening ?