代码之家  ›  专栏  ›  技术社区  ›  Ajay Kulkarni

未解析的引用。由于接收器类型不匹配,以下任何候选项都不适用

  •  1
  • Ajay Kulkarni  · 技术社区  · 6 年前

    我在用 androidx navigation architecture 随着 Kotlin 1.2.71 在Android Studio 3.2.1中。我的片段代码是:

    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()
        }
    }  
    

    布局代码为:

    <?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>  
    

    我的build.gradle是:

    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()
    }
    

    当我试图运行我的代码时,我得到以下错误:

    未解析的引用。由于接收器类型不匹配,以下任何候选项都不适用: public val activity.detailbutton:按钮!在kotlinx.android.synthetic.main.fragment中定义欢迎 public val dialog.detailbutton:按钮!在kotlinx.android.synthetic.main.fragment中定义欢迎 public val android.app.fragment.detailbutton:按钮!在kotlinx.android.synthetic.main.fragment中定义欢迎 public val androidx.fragment.app.fragment.detailButton:按钮!在kotlinx.android.synthetic.main.fragment中定义欢迎 public val layoutContainer.detailButton:按钮!在kotlinx.android.synthetic.main.fragment中定义欢迎

    如何解决该错误?

    2 回复  |  直到 6 年前
        1
  •  1
  •   Karan Mer    6 年前

    您正在访问的按钮位于您的碎片内,但您正在直接访问它。您应该通过父视图访问它,就像我们使用Java一样 rootview.findViewById() 所以用

    view.detailButton.setOnClickListener {
        findNavController().navigate(R.id.action_welcomeFragment_to_detailsFragment)
    }
    
        2
  •  0
  •   CoolMind    6 年前

    在我的例子中,这个函数导致了错误:

    private fun showProgress(view: View? = null) {
        (view?.progressBar ?: progressBar)?.visibility = View.VISIBLE
    }
    

    Android Studio提供了包含 progressBar 我选择了一个。但是 view?.progressBar 继续变红。然后我缩短了标题:

    private fun showProgress(view: View) .

    只有在提供的布局之后。然后我又写了: private fun showProgress(view: View? = null) .