未解决的参考:FragmentTitleBinding

Unresolved Reference : FragmentTitleBinding

我想在我的 android 应用程序中使用片段,但我无法在我的片段中导入 FragmentTitleBinding class 我尝试了互联网上所有可能的解决方案

  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 但它给出了一个错误 unresolved reference

由于数据绑定的执行方式类似于注释处理器,因此您必须清理项目然后重新构建。如果那样行不通。试试这个: 1.点击文件菜单 2. Select 使缓存无效/重新启动

希望有用。请注意,您的数据绑定 class 与您的布局名称

同名

更新

确保你的依赖项中有这些

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

android{
dataBinding {
        enabled = true
    }
}

你还需要检查SDK工具中的Repository

这个问题让我抓狂,在尝试了上述修复之后问题仍然存在。这很难,因为如果您正在阅读此博客,很可能您正在学习“Android Kotlin 基础知识 03.1:创建片段”在线课程。你最不需要的就是在学习时出现无法解释的错误。

在我的例子中,错误是 IDE 在导入堆栈的顶部添加了“导入 android.R”。删除该导入代码,问题就消失了。

在 Android Studio 中有一条信息性消息与有问题的“导入 android.R”代码相关联,内容为;

不要在此处包含 android.R;为每个用法使用完全限定名称

检查信息:导入android.R通常不是故意的;当您使用 IDE 并要求它在项目的 R class 不存在时自动添加导入时,有时会发生这种情况。 一旦导入完成,您可能会收到很多 "confusing" 错误消息,因为 android.R 上可用的字段当然不是您仅查看自己的 R class 所期望的字段.

问题 ID:Suspiciousimport

我希望这对像我这样的其他学习者有所帮助

请在 android 中添加 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>

现在转到构建 -> 清理项目构建 -> 重建项目。如果仍然出现错误,则 File -> Invalidate caches/restart.

你也可以看看这个帖子。 https://github.com/udacity/andfun-kotlin-android-trivia/issues/9