Unresolved reference. None of the following candidates is applicable because of receiver type mismatch

32,045

Solution 1

Button you are accessing is inside your fragement but your are accessing it directly. You should access it via parent view like we do with java rootview.findViewById() so use

view.detailButton.setOnClickListener {
    findNavController().navigate(R.id.action_welcomeFragment_to_detailsFragment)
}

Solution 2

I have faced the same problem with my adapter class and finally problem solved by adding the following code to build.gradle(Module: app) file of the project.

apply plugin: 'org.jetbrains.kotlin.android.extensions'
androidExtensions {
    experimental = true
}

Since this new version of Kotlin, the Android Extensions have incorporated some new interesting features like caches in any class

Solution 3

I had this issue and figured out that the root cause was play-services with versions 17.x.x

My fix

Change

google_android_gms_version

in

implementation "com.google.android.gms:play-services-location:$google_android_gms_version"
    implementation "com.google.android.gms:play-services-gcm:$google_android_gms_version"

from 17.x.x to 15.0.1 The reason is that play services 17.x.x has dependencies on Androidx

General way Click the Gradle option in the extreme right panel, Go to app -> help ->dependencies Search androidx in the chart. Find out which library has it. Change or modify your gradle accordingly.

Those who can migrate to androidx, that well and good. If you have to keep it at appcompat, then follow the answer.

Share:
32,045
Ajay Kulkarni
Author by

Ajay Kulkarni

#SOreadytohelp Rules Smash keyboard. Come up with beautiful products, no matter what happens I am nerdier than 97% of all people. Are you a nerd? Click here to take the Nerd Test, get nerdy images and jokes, and talk on the nerd forum! http://www.nerdtests.com/images/ft/nq/4fbf3d9fe6.gif https://akulkarni9.github.io/#/

Updated on July 09, 2022

Comments

  • Ajay Kulkarni
    Ajay Kulkarni almost 2 years

    I'm using androidx navigation architecture along with Kotlin 1.2.71 in Android studio 3.2.1. My fragment code is:

    package com.dell.andnav.fragments
    
    import android.os.Bundle
    import android.support.v4.app.Fragment
    import android.view.LayoutInflater
    import android.view.View
    import android.view.ViewGroup
    import android.widget.Button
    import androidx.navigation.fragment.findNavController
    import com.dell.andnav.R
    import kotlinx.android.synthetic.main.fragment_welcome.*
    
    
    /**
     * A simple [Fragment] subclass.
     * Use the [WelcomeFragment.newInstance] factory method to
     * create an instance of this fragment.
     *
     */
    class WelcomeFragment : Fragment() {
    
        override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                                  savedInstanceState: Bundle?): View? {
            // Inflate the layout for this fragment
            return inflater.inflate(R.layout.fragment_welcome, container, false)
        }
    
        override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
            super.onViewCreated(view, savedInstanceState)
            detailButton.setOnClickListener {
                findNavController().navigate(R.id.action_welcomeFragment_to_detailsFragment)
            }
        }
    
        companion object {
            @JvmStatic
            fun newInstance() = WelcomeFragment()
        }
    }  
    

    And layout code is:

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/frameLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".fragments.WelcomeFragment">
    
    
        <Button
            android:id="@+id/detailButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/to_detail_fragment"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    
    </android.support.constraint.ConstraintLayout>  
    

    My build.gradle is:

    apply plugin: 'com.android.application'
    apply plugin: 'kotlin-android'
    apply plugin: 'kotlin-android-extensions'
    apply plugin: 'androidx.navigation.safeargs'
    
    android {
        compileSdkVersion 28
        defaultConfig {
            applicationId "com.dell.andnav"
            minSdkVersion 21
            targetSdkVersion 28
            versionCode 1
            versionName "1.0"
            testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
            androidExtensions {
                experimental = true
            }
        }
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            }
        }
    }
    
    dependencies {
        def nav_version = "1.0.0-alpha07"
        implementation fileTree(dir: 'libs', include: ['*.jar'])
    //    implementation 'com.android.support:appcompat-v7:28.0.0'
        implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    //    implementation 'com.android.support:support-v4:28.0.0'
        testImplementation 'junit:junit:4.12'
        androidTestImplementation 'com.android.support.test:runner:1.0.2'
        androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    
        implementation 'androidx.lifecycle:lifecycle-extensions:2.0.0'
        implementation 'androidx.legacy:legacy-support-v4:1.0.0'
        implementation 'androidx.multidex:multidex:2.0.0'
        implementation 'androidx.appcompat:appcompat:1.0.2'
    
        implementation "android.arch.navigation:navigation-fragment-ktx:$nav_version"
        implementation "android.arch.navigation:navigation-ui-ktx:$nav_version"
    
        implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    }
    repositories {
        mavenCentral()
    }
    

    When I try to run my code, I get below error:

    Unresolved reference. None of the following candidates is applicable because of receiver type mismatch: public val Activity.detailButton: Button! defined in kotlinx.android.synthetic.main.fragment_welcome public val Dialog.detailButton: Button! defined in kotlinx.android.synthetic.main.fragment_welcome public val android.app.Fragment.detailButton: Button! defined in kotlinx.android.synthetic.main.fragment_welcome public val androidx.fragment.app.Fragment.detailButton: Button! defined in kotlinx.android.synthetic.main.fragment_welcome public val LayoutContainer.detailButton: Button! defined in kotlinx.android.synthetic.main.fragment_welcome

    How can I resolve that error?