未解决的参考:FragmentTitleBinding

时间:2019-08-01 07:06:53

标签: android android-fragments kotlin android-databinding

我想在我的android应用程序中使用片段,但是我无法在我的片段类中导入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,但它给出了一个未解决的参考错误

3 个答案:

答案 0 :(得分:4)

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

image

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

更新

确保您的依赖项中包含这些内容

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

android{
dataBinding {
        enabled = true
    }
}

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

答案 1 :(得分:0)

这个问题使我发疯,即使尝试了上述修复程序,问题仍然存在。这很困难,因为如果您正在阅读此博客,则很可能正在通过“ Android Kotlin基础知识03.1:创建片段”在线课程。您需要做的最后一件事是在学习时出现无法解释的错误。

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

在Android Studio中,有一条提示性消​​息,该消息与有问题的“ import android.R”代码关联;

此处不包含android.R;每次使用时都使用完全限定的名称

检查信息:导入android.R通常不是故意的;当您使用IDE并要求其在不存在项目的R类时自动添加导入时,有时会发生这种情况。 一旦导入到那里,您可能会收到很多“令人困惑”的错误消息,因为当然android.R上的可用字段不是您只看自己的R类所期望的。

问题ID:可疑导入

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

答案 2 :(得分:0)

请将此文件添加到build.gradle(app)

中的 android中
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>

现在转到构建->清理项目构建->重建项目。如果仍然出现错误,请文件->使缓存无效/重新启动

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