未解决的引用:FragmentTitleBinding

6

我想在我的安卓应用程序中使用碎片(Fragment),但是我无法在我的碎片类中导入FragmentTitleBinding。我尝试了互联网上所有可能的解决方案。

  1. 我检查了xml文件中的布局标签
  2. 我编辑了build.gradle文件
  3. 同步项目
import com.example.android.navigation.databinding.FragmentTitleBinding

class TitleFragment : Fragment() {

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                          savedInstanceState: Bundle?): View? {
    val binding : com.example.android.navigation.databinding.FragmentTitleBinding=
            DataBindingUtil.inflate(inflater,R.layout.fragment_title,container,false)
    return binding.root;
    }
}

片段化XML

 <?xml version="1.0" encoding="utf-8"?>

<layout 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"
    tools:context="com.example.android.navigation.TitleFragment">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/titleConstraint"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <Button
            android:id="@+id/playButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="@dimen/horizontal_margin"
            android:layout_marginEnd="@dimen/horizontal_margin"
            android:paddingStart="@dimen/button_padding"
            android:paddingEnd="@dimen/button_padding"
            android:text="Play"
            android:textColor="@color/colorAccent"
            android:textSize="@dimen/button_text_size"
            android:textStyle="bold"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/titleImage" />

            <ImageView
            android:id="@+id/titleImage"
            android:layout_width="0dp"
            android:layout_height="@dimen/image_header_height"
            android:layout_marginStart="@dimen/horizontal_margin"
            android:layout_marginEnd="@dimen/horizontal_margin"
            android:scaleType="fitCenter"
            app:layout_constraintBottom_toTopOf="@+id/playButton"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="1.0"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:srcCompat="@drawable/android_trivia" />

    </androidx.constraintlayout.widget.ConstraintLayout>
</layout>

build.gradle

  apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
apply plugin: 'kotlin-android-extensions'

android {
    compileSdkVersion 28
    dataBinding {
        enabled = true
    }
    defaultConfig {
        applicationId 'com.example.android.navigation'
        minSdkVersion 19
        targetSdkVersion 28
        vectorDrawables.useSupportLibrary = true
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    productFlavors {
    }
    buildToolsVersion = '28.0.3'
}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    // Kotlin
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$version_kotlin"
    // Constraint Layout
    implementation "androidx.constraintlayout:constraintlayout:$version_constraint_layout"
    // Core
    implementation "androidx.core:core:$version_core"
    // Material Design
    implementation "com.google.android.material:material:$version_material"
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    kapt 'com.android.databinding:compiler:3.3.0'
}
kapt {
    generateStubs = true
}

我想解决 FragmentTitleBinding,但它报错为未解决的引用。

请展示你的R.layout.fragment_title - Tidder
@Tidder 请现在检查。 - AndroidDev
1
尝试在布局文件中的<layout>...</layout>标签内添加一个<data>标签,就像文档中所示:https://developer.android.com/topic/libraries/data-binding/expressions.html - Tidder
仍然无法工作 - AndroidDev
4个回答

16

由于数据绑定的表现类似注解处理器,您需要清理项目并重新构建。 如果这种方法不起作用,请尝试以下操作: 1. 点击文件菜单 2. 选择使缓存无效/重新启动

图像

我希望它能起作用。请注意,您的数据绑定类与布局名称相同。

更新

确保您的依赖项中包含以下内容

kapt "androidx.lifecycle:lifecycle-compiler:2.0.0"

android{
dataBinding {
        enabled = true
    }
}

你还需要在SDK工具中检查存储库 sdk tools


1
实际上,在Jetpack中已经包含了数据绑定(kapt“androidx.lifecycle:lifecycle-compiler:2.0.0”),因此您不再需要这个依赖项kapt 'com.android.databinding:compiler:3.3.0'。 - Nanda Z
1
最终通过重新构建项目,它成功了,之前它不起作用,但现在它可以工作了 :) - AndroidDev
清除缓存后,我的Kotlin文件上出现了很多红线,项目无法编译了 :( - jap1968
@jap1968 在失效和重启后,您必须连接到互联网,一些缺失的库将重新检查。还要确保您没有gradle问题。 - Nanda Z
通过执行“清理项目”和“重新构建项目”解决了问题。 - jap1968
显示剩余5条评论

2
这个问题让我很困惑,即使尝试了上面的解决方法,问题仍然存在。这很困难,因为如果你正在阅读这篇博客,很可能是在学习“Android Kotlin基础03.1:创建片段”在线课程。当你在学习时,最不需要的是一个无法解释的错误。
在我的情况下,问题出在IDE添加了“import android.R”到导入堆栈的顶部。删除该导入代码,问题就会消失。
在Android Studio中,与有问题的“import android.R”代码相关联的是一个信息消息,其中包含以下内容:
不要在此处包含android.R;对于每个使用,都使用完全限定名称
检查信息:导入android.R通常是无意的;它有时会在您使用IDE并在项目的R类不存在的时间要求自动添加导入时发生。一旦有了导入,您可能会收到很多“混淆”的错误消息,因为android.R上可用的字段当然不是您从自己的R类中看到的那些字段。
问题ID:可疑的导入
希望这能帮助像我这样的其他学习者

非常感谢。我花了一个小时拔头发,想知道为什么我一直得到“未解决的引用”! - ctd

1
通常情况下,当我们创建fragment_title时,默认创建的文件是 ->enter image description here
因此,尝试将 更改为 ,然后可以避免FragmentTitleBinding错误。 enter image description here

如果您在此方面遇到任何问题,请告诉我。 - bibek

0
请在 android in build.gradle(app) 中添加此内容。
buildFeatures {
        dataBinding = true
    } 

以下是一些选项,你可以尝试。 在创建空的TitleFragment.kt之后,进入fragment_title.xml,将其替换为:
<layout 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"
    tools:context="com.example.testfragment.TitleFragment">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/titleConstraint"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

       <!-- your xml tag-->

        

    </androidx.constraintlayout.widget.ConstraintLayout>
</layout>

现在进入 Build -> Clean projectBuild -> Rebuild Project。如果仍然出现错误,则进入 File -> Invalidate caches/restart

您也可以查看此线程。 https://github.com/udacity/andfun-kotlin-android-trivia/issues/9


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接