Android DataBinding类未生成求助:迁移findViewById示例遇阻
Hey there, let's get your DataBinding class generated properly. I spotted several issues in your configuration and layout that are causing the problem. Here's how to fix them step by step:
1. Fix Root build.gradle Dependencies
Your root build script has conflicting and outdated dependencies that break DataBinding support:
- You've included two versions of the Android Gradle Plugin (
3.0.1and1.3.0) — this will cause version conflicts. Stick to the newer one. - The old
com.android.databinding:dataBinderdependency is obsolete. Starting with Android Gradle Plugin 3.0+, DataBinding is integrated directly into the core plugin, so you don't need this separate classpath entry.
Here's the corrected root build.gradle:
buildscript { repositories { google() jcenter() } dependencies { classpath 'com.android.tools.build:gradle:3.0.1' // Removed duplicate Gradle plugin and obsolete dataBinder dependency } } allprojects { repositories { google() jcenter() } } task clean(type: Delete) { delete rootProject.buildDir }
2. Enable DataBinding in Module-level build.gradle
You're missing the critical step of enabling DataBinding in your app module's build file. Open your app/build.gradle and add this inside the android block:
android { // ... other existing config (compileSdkVersion, defaultConfig, etc.) dataBinding { enabled = true } }
3. Fix Layout File Syntax Errors
Your layout has two syntax issues that prevent the binding class from being generated:
- The
<variable>tag isn't properly closed (missing/at the end). - You've duplicated the
toolsnamespace declaration in the root<layout>and inner<LinearLayout>tags — remove the duplicate.
Here's the corrected layout:
<?xml version="1.0" encoding="utf-8"?> <layout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"> <data class="SudokuBinding"> <variable name="user" type="com.example.flash.sudoku.Sudoku" /> </data> <LinearLayout xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/background" android:orientation="horizontal" android:padding="30dip" tools:context="com.example.flash.sudoku.Sudoku"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:orientation="vertical"> <TextView android:id="@+id/textView" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="25dip" android:gravity="center" android:text="@string/main_title" android:textSize="24.5sp" /> <Button android:id="@+id/continue_button" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/continue_label" /> <Button android:id="@+id/new_button" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/new_game_label" /> <Button android:id="@+id/about_button" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/about_label" /> <Button android:id="@+id/exit_button" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/exit_label" /> </LinearLayout> </LinearLayout> </layout>
4. Clean and Rebuild Your Project
After making all these changes:
- Go to Build > Clean Project to clear old build artifacts.
- Then run Build > Rebuild Project to trigger the DataBinding class generation.
Once the rebuild finishes, you should be able to access the SudokuBinding class in your Activity code.
内容的提问来源于stack exchange,提问作者Tom Magaro

