I am developing new app when I run project I am getting following java.lang.ClassCastException: com.example.runtrackerapp.ui.MainActivity cannot be cast to com.example.runtrackerapp.databinding.ActivityMainBinding at com.example.runtrackerapp.ui.fragments.SetupFragment.writePersonalDataToSharedPref(SetupFragment.kt:84) at com.example.runtrackerapp.ui.fragments.SetupFragment.onViewCreated$lambda-0(SetupFragment.kt:60) at com.example.runtrackerapp.ui.fragments.SetupFragment.$r8$lambda$4d3caNvVygzMMPPCQbih5sKklFY(Unknown Source:0) at com.example.runtrackerapp.ui.fragments.SetupFragment$$ExternalSyntheticLambda0.onClick(Unknown Source:2) at android.view.View.performClick(View.java:6614) at android.view.View.performClickInternal(View.java:6587) at android.view.View.access$3100(View.java:787) at android.view.View$PerformClick.run(View.java:26122) at android.os.Handler.handleCallback(Handler.java:873) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:201)
below my SetupFragment.kt where exception occuring
@AndroidEntryPoint
class SetupFragment : Fragment() {
private var _binding: FragmentSetupBinding? = null
private val binding get() = _binding!!
@Inject
lateinit var sharedPref: SharedPreferences
@set:Inject
var isFirstAppOpen = true
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View {
// inflate the layout and bind to the _binding
_binding = FragmentSetupBinding.inflate(inflater, container, false)
return binding.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
if(!isFirstAppOpen){
val navOptions = NavOptions.Builder()
.setPopUpTo(R.id.setupFragment, true)
.build()
findNavController().navigate(
R.id.action_setupFragment_to_runFragment,
savedInstanceState,
navOptions)
}
binding.tvContinue.setOnClickListener {
val success = writePersonalDataToSharedPref()
if (success){
findNavController().navigate(R.id.action_setupFragment_to_runFragment)
}else{
Snackbar.make(requireView(), "Please enter all the fields", Snackbar.LENGTH_SHORT).show()
}
}
}
private fun writePersonalDataToSharedPref(): Boolean {
val name = binding.etName.text.toString()
val weight = binding.etWeight.text.toString()
if(name.isEmpty() || weight.isEmpty()) {
return false
}
sharedPref.edit()
.putString(KEY_NAME, name)
.putFloat(KEY_WEIGHT, weight.toFloat())
.putBoolean(KEY_FIRST_TIME_TOGGLE, false)
.apply()
val toolbarText = "Let's go, $name!"
(requireActivity() as ActivityMainBinding).tvToolbarTitle.text = toolbarText
return true
}
}
I want to know exactly where I am making mistake
CodePudding user response:
(requireActivity() as MainActivity).tvToolbarTitle.text = toolbarText
or
(requireActivity() as MainActivity).binding.tvToolbarTitle.text = toolbarText
error is with ActivityMainBinding this line add your activity name here like (MainActivity)
CodePudding user response:
(requireActivity() as ActivityMainBinding)
This is the problem statement .. You can not cast Activity to a Binding Object because they are not related in any way ..
Although this is a wrong approach IMO . for now to make it work you can make your binding object public . and access it as below .
(requireActivity() as MainActivity).binding.tvToolbarTitle.text = toolbarText
For a better implementation you can use a SharedViewModel
. Or have the toolbar in fragment itself if possible.