未解析的引用.由于接收器类型不匹配,以下候选者均不适用



我在Android studio 3.2.1中使用androidx navigation architectureKotlin 1.2.71。我的碎片代码是:

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:按钮!定义于kotlin.android.synthetic.main.fragment_welcomepublic val Dialog.detailButton:按钮!定义于kotlin.android.synthetic.main.fragment_welcomepublic val android.app.Fragment.detailButton:按钮!定义于kotlin.android.synthetic.main.fragment_welcomepublic val androidx.fragment.app.fragment.detailButton:按钮!定义于kotlin.android.synthetic.main.fragment_welcomepublic val LayoutContainer.detailButton:按钮!定义于kotlin.android.synthetic.main.fragment_welcome

如何解决该错误?

您正在访问的按钮位于片段内,但您正在直接访问它。您应该像使用javarootview.findViewById()那样通过父视图访问它,所以使用

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

我的适配器类也遇到了同样的问题,最终通过添加以下代码来构建项目的.gradle(Module:app(文件来解决问题。

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

自从这个新版本的Kotlin以来,Android扩展已经包含了一些新的有趣的功能,比如任何类中的缓存

我遇到了这个问题,并发现根本原因是使用版本17.x.x 的播放服务

我的修复

更改

google_android_gms_version

在中

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

17.x.x15.0.1原因是播放服务17.x.x依赖Androidx

常规方式单击最右侧面板中的渐变选项,转到应用程序->帮助->依赖项在图表中搜索androidx。找出哪个库有它。相应地更改或修改你的等级。

那些可以迁移到androidx的人,那很好。如果您必须将其保存在appcompat中,请按照答案进行操作。

使用androidx,您必须使用itemView 访问

itemView.detailButton

import.view.*喜欢下面的

import kotlinx.android.synthetic.main.fragment_welcome.view.*

而不是

import kotlinx.android.synthetic.main.fragment_welcome.*

我有两个关于这个错误的问题。

  1. 我在扩展中混合了接收器类型。

    import android.widget.Toolbar
    fun Toolbar.setMenuItem() : Boolean {
    ...
    }
    

然后尝试将此函数应用于布局中的Toolbar。但在XML中是androidx.appcompat.widget.Toolbar

  1. 此函数导致错误:

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

AndroidStudio提供了包含progressBar的布局,我选择了一个。但view?.progressBar仍然是红色的。然后我缩短了一个标题:

private fun showProgress(view: View)

只有在那之后,AS才提供布局。然后我再次写道:private fun showProgress(view: View? = null)

最新更新