You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android新手求助:Kotlin DataBinding无法工作,存在未解析引用错误

Fixing DataBinding "Unresolved Reference" Errors in Your Android Fragment

Hey there! Let's work through this DataBinding issue step by step—you're super close, just a few key tweaks needed to get everything working.

1. Wrap Your XML Layout in a <layout> Tag

This is the most common culprit when DataBinding classes don't generate! DataBinding requires your layout file to be wrapped in a root <layout> element so the build tools know to generate the binding class. Your current fragment_login.xml uses LinearLayout as the root, which skips this critical generation step.

Update your XML to look like this:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:layout_gravity="center_vertical"
        tools:context="com.example.project2019.fragment_login">
        
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/hello_blank_fragment"
            android:layout_gravity="center_horizontal" />
        
        <Button
            android:id="@+id/loginbtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:text="Log In" />
    </LinearLayout>
</layout>

2. Fix Your Fragment Class Naming (Kotlin Convention)

Kotlin class names follow UpperCamelCase convention, but your fragment is named fragment_login (lowercase start). This can cause mismatches with the auto-generated binding class (which uses the layout name converted to UpperCamelCase: FragmentLoginBinding).

Rename your fragment class to FragmentLogin:

package com.example.project2019
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import com.example.project2019.databinding.FragmentLoginBinding

class FragmentLogin : Fragment() {
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val binding = DataBindingUtil.inflate<FragmentLoginBinding>(inflater, R.layout.fragment_login, container, false)
        return binding.root
    }
}

Don't forget to update any references to this fragment in your code or manifest too!

3. Clean & Rebuild Your Project

DataBinding classes are generated at compile time, so old build cache can sometimes block their creation. Follow these steps:

  • Go to Build > Clean Project in Android Studio
  • Then go to Build > Rebuild Project
  • If that still doesn't work, try File > Invalidate Caches... > Invalidate and Restart to clear Android Studio's cached data.

4. Double-Check Your Gradle Configuration

Your build.gradle looks mostly correct, but just confirm:

  • You have the kotlin-kapt plugin applied (you do, great!)
  • dataBinding.enabled = true is inside the android block (you have that too)
  • Make sure you've clicked the "Sync Now" button in Android Studio after making any Gradle changes to apply them.

After making these changes, the FragmentLoginBinding class should generate correctly, and your import errors should disappear.

内容的提问来源于stack exchange,提问作者Stephan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 08:22:52