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

Android Data Binding报错:DashboardActivity无法解析inflate和getRoot方法

问题描述

我开发了一个简单的Android应用,通过Intent从activity_main.xml跳转至activity_dashboard.xml。在DashboardActivity.java中使用Data Binding特性时,出现两个错误:

  • Cannot resolve method 'inflate' in 'DashboardActivity'
  • Cannot resolve method 'getRoot' in 'DashboardActivity'

已修改build.gradle文件,且Data Binding在MainActivity.java中可正常工作,但在DashboardActivity中失效。尝试过以下操作:

  • 重新同步Gradle文件
  • 清理缓存

相关代码

1. DashboardActivity.java

package com.example.inclasstask5;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.example.inclasstask5.databinding.ActivityDashboardBinding;

public class DashboardActivity extends AppCompatActivity {

    SharedPreferences sharedPref;
    TextView enteredData;

    DashboardActivity binding;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DashboardActivity.inflate(getLayoutInflater());
        View view1 = binding.getRoot();
        setContentView(view1);
//        setContentView(R.layout.activity_dashboard);
        sharedPref = getSharedPreferences("login_Details", Context.MODE_PRIVATE);
        enteredData = findViewById(R.id.textView2);
        init();
    }

    private void init(){
        String username = sharedPref.getString("ID" , "No Username");
        enteredData.setText("Login Successfully - " + username);

    }

}

2. activity_dashboard.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
    android:id="@+id/root2"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".DashboardActivity">

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="140dp"
        android:layout_marginTop="115dp"
        android:text=""
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

解决方案

错误核心是绑定对象的类型声明与调用错误,修复步骤如下:

  1. 修正绑定变量的类型:将DashboardActivity改为自动生成的绑定类ActivityDashboardBinding

    // 错误写法
    // DashboardActivity binding;
    // 正确写法
    ActivityDashboardBinding binding;
    
  2. 修正inflate方法的调用主体:用ActivityDashboardBinding类调用,而非DashboardActivity

    // 错误写法
    // binding = DashboardActivity.inflate(getLayoutInflater());
    // 正确写法
    binding = ActivityDashboardBinding.inflate(getLayoutInflater());
    
  3. 优化代码:通过绑定对象直接访问布局控件,无需findViewById
    移除原有的TextView enteredData声明及findViewById调用,直接使用binding.textView2访问控件。

修复后的完整DashboardActivity.java代码

package com.example.inclasstask5;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;

import com.example.inclasstask5.databinding.ActivityDashboardBinding;

public class DashboardActivity extends AppCompatActivity {

    SharedPreferences sharedPref;
    ActivityDashboardBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityDashboardBinding.inflate(getLayoutInflater());
        View view1 = binding.getRoot();
        setContentView(view1);
        sharedPref = getSharedPreferences("login_Details", Context.MODE_PRIVATE);
        init();
    }

    private void init(){
        String username = sharedPref.getString("ID" , "No Username");
        binding.textView2.setText("Login Successfully - " + username);
    }

}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.27 18:58:12