这是一个简单的应用程序。我正在使用 Intent 从 Activity_main.xml 导航到 Activity_dashboard.xml 在 DashboardActivity.java 文件中,我尝试使用数据绑定属性。但我收到了 2 个错误
我在 build.gradle 文件中进行了更改。此外,数据绑定在 MainActivity.java 中工作正常,但在 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);
}
}
<?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>
您应该将绑定变量声明为
ActivityDashboardBinding binding;
而不是
DashboardActivity binding;