0

This is a simple application. I am using Intent to navigate from activity_main.xml to activity_dashboard.xml In DashboardActivity.java file I am trying to use Data binding property. But I am getting 2 errors

  1. Cannot resolve method 'inflate' in 'DashboardActivity'
  2. Cannot resolve method 'getRoot' in 'DashboardActivity'

I have made changes in the build.gradle file. Also data binding is working fine in MainActivity.java but not in DashboardActivity.java

  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); } } 
  1. 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. sync the gradle files again
  2. cleared caches
2
  • Did you follow the steps here? Why aren't you using the binding class and inflating it? Commented Mar 15, 2024 at 5:17
  • binding declaration type should be ActivityDashboardBinding Commented Mar 15, 2024 at 6:03

1 Answer 1

0

You should declare binding variable as

ActivityDashboardBinding binding; 

instead of

DashboardActivity binding; 

And in activity's onCreate method assign

binding = ActivityDashboardBinding.inflate(getLayoutInflater()); 

instead of

binding = DashboardActivity.inflate(getLayoutInflater()); 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.