Android 未解析的引用:碎片标题绑定
我想在我的android应用程序中使用片段,但我无法在我的片段类中导入Android 未解析的引用:碎片标题绑定,android,android-fragments,kotlin,android-databinding,Android,Android Fragments,Kotlin,Android Databinding,我想在我的android应用程序中使用片段,但我无法在我的片段类中导入片段标题绑定,我在互联网上尝试了所有可能的解决方案 我检查了xml文件中的布局标记 我编辑build.gradle文件 同步项目 片段XML 我想解析片段标题绑定,但它给出了一个未解析引用的错误,因为数据绑定的执行方式与注释处理器类似,所以您必须清理项目,然后重新构建。如果那样不行的话。试试这个: 1.单击“文件”菜单 2.选择“使缓存失效/重新启动” 我希望它能起作用。请注意,数据绑定类的名称与布局名称相同 更新 确保在依
片段标题绑定
,我在互联网上尝试了所有可能的解决方案
我想解析
片段标题绑定
,但它给出了一个未解析引用的错误,因为数据绑定的执行方式与注释处理器类似,所以您必须清理项目,然后重新构建。如果那样不行的话。试试这个:
1.单击“文件”菜单
2.选择“使缓存失效/重新启动”
我希望它能起作用。请注意,数据绑定类的名称与布局名称相同
更新
确保在依赖项中包含这些内容
kapt "androidx.lifecycle:lifecycle-compiler:2.0.0"
及
您还需要检查SDK工具中的存储库
这个问题让我发疯,即使尝试了上述修复,问题仍然存在。这很难,因为如果你正在阅读这篇博客,你很可能正在学习“Android Kotlin基础03.1:创建片段”在线课程。当你学习时,你最不需要的就是一个无法解释的错误 在我的例子中,错误在于IDE在导入堆栈的顶部添加了“import android.R”。删除该导入代码,问题就会消失 在Android Studio中,有一条信息消息与有问题的“import Android.R”代码关联,该代码如下: 这里不要包括android.R;对每个用法使用完全限定的名称 检验信息:导入android.R通常不是有意的;当您使用IDE并要求它在项目的R类不存在时自动添加导入时,有时会发生这种情况。 导入完成后,您可能会收到很多“令人困惑”的错误消息,因为android.R上的可用字段当然不是您仅查看自己的R类所期望的字段 问题id:可疑导入
我希望这有助于像我这样的其他学习者请将此添加到android in build.gradle(应用程序) 这里有以下选项,您可以尝试。 创建空白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>
请显示您的R.layout.fragment\u title
@Tidder请立即检查并尝试在..
标签中向布局文件添加
标签,如文档中所示:仍不工作没有类型?在这个数据绑定中{enabled=true}?实际上在jetpack中已经包含了数据绑定(kapt“androidx.lifecycle:lifecycle compiler:2.0.0”),你不再需要这个依赖关系了kapt'com.android.dataBinding:compiler:3.3.0'最后它通过重建项目工作了它以前不工作,但现在工作了:)为什么我们需要kapt“androidx.lifecycle:lifecycle编译器:”
?好吧,只有在代码中有与生命周期相关的注释时才需要它,特别是@OnLifecycleEvent。非常感谢。我花了一个小时的时间整理我的头发,想知道为什么我一直得到“未解决的引用”!
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
}
kapt "androidx.lifecycle:lifecycle-compiler:2.0.0"
android{
dataBinding {
enabled = true
}
}
buildFeatures {
dataBinding = true
}
<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>