我想在我的 android 应用程序中使用 fragment ,但我无法在 fragment 类中导入 FragmentTitleBinding
我在互联网上尝试了所有可能的解决方案
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;
}
}
fragment 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>
构建.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 引用 最佳答案
由于数据绑定(bind)的性能类似于注释处理器,因此您必须清理项目然后再次重建。如果这种方式行不通。试试这个:
1.点击文件菜单
2. 选择无效缓存/重启
我希望它有效。请注意您的数据绑定(bind)类与您的布局名称具有相同的名称
更新
确保您的依赖项中有这些
kapt "androidx.lifecycle:lifecycle-compiler:2.0.0"
和
android{
dataBinding {
enabled = true
}
}
您还需要在 SDK 工具中检查 Repository
关于android - Unresolved reference : FragmentTitleBinding,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57303513/