android-fragments 无法将y强制转换为com.example.runtrackerapp.数据绑定.ActivityMainBinding?

ivqmmu1c  于 2022-11-14  发布在  Android
关注(0)|答案(2)|浏览(107)

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

pu3pd22g

pu3pd22g1#

(requireActivity() as MainActivity).tvToolbarTitle.text = toolbarText

(requireActivity() as MainActivity).binding.tvToolbarTitle.text = toolbarText

错误在于ActivityMainBinding此行,请在此处添加您活动名称,如(MainActivity)

epggiuax

epggiuax2#

(requireActivity() as ActivityMainBinding)这是一个问题语句。您不能将Activity强制转换为绑定对象,因为它们在任何方面都不相关。
虽然这是一个错误的方法IMO。现在,为了使它工作,你可以使你的绑定对象公开。并访问它如下。

(requireActivity() as MainActivity).binding.tvToolbarTitle.text = toolbarText

为了更好的实现,你可以使用SharedViewModel。或者如果可能的话,把工具栏本身放在fragment中。

相关问题