Home > Software design >  FindNavController - None of the following candidates is applicable because of receiver type mismatch
FindNavController - None of the following candidates is applicable because of receiver type mismatch

Time:01-23

I've been doing this Android Studio course and I've stumbled upon a problem with this particular method. For whatever reason, I can use it in every fragment except one, in which apparently can't be imported.

Here is the fragment: (StartFragment)

package com.example.cupcake

import android.app.Fragment
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.navigation.fragment.findNavController
import com.example.cupcake.databinding.FragmentStartBinding

/**
 * This is the first screen of the Cupcake app. The user can choose how many cupcakes to order.
 */
class StartFragment : Fragment() {

    // Binding object instance corresponding to the fragment_start.xml layout
    // This property is non-null between the onCreateView() and onDestroyView() lifecycle callbacks,
    // when the view hierarchy is attached to the fragment.
    private var binding: FragmentStartBinding? = null

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val fragmentBinding = FragmentStartBinding.inflate(inflater, container, false)
        binding = fragmentBinding
        return fragmentBinding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        binding?.apply {
            // Set up the button click listeners
            orderOneCupcake.setOnClickListener { orderCupcake(1) }
            orderSixCupcakes.setOnClickListener { orderCupcake(6) }
            orderTwelveCupcakes.setOnClickListener { orderCupcake(12) }
        }
    }

    /**
     * Start an order with the desired quantity of cupcakes and navigate to the next screen.
     */
    fun orderCupcake(quantity: Int) {
        findNavController().navigate(R.id.action_startFragment_to_flavorFragment) //here is the error
    }

    /**
     * This fragment lifecycle method is called when the view hierarchy associated with the fragment
     * is being removed. As a result, clear out the binding object.
     */
    override fun onDestroyView() {
        super.onDestroyView()
        binding = null
    }
}

The whole code is in the code link, thanks

CodePudding user response:

Your fragment extends android.app.Fragment - that's the deprecated, should never be used framework fragment. You need to replace your import android.app.Fragment with import androidx.fragment.app.Fragment to actually extend the AndroidX Fragment you need to use with Navigation.

  •  Tags:  
  • Related