我是 Kotlin 新手。我正在尝试从 Firebase 身份验证中获取电子邮件,以将其显示在菜单上,以显示从哪个电子邮件用户登录。我收到了变量中的电子邮件。
尝试通过方法textview.text或textview.setText或其他方法从MainActivity.kt更改TextView的文本后,我的应用程序甚至无法运行。
MainActivity.kt:
class MainActivity : AppCompatActivity() {
private lateinit var firebaseAuth: FirebaseAuth
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val binding: MainactivityBinding = MainactivityBinding.inflate(layoutInflater)
setContentView(binding.root)
var email_textview = findViewById<TextView>(R.id.text_email)
val drawerLayout = findViewById<DrawerLayout>(R.id.drawerLayout)
val buttonDrawerToggle = findViewById<ImageButton>(R.id.buttonDrawerToggle)
val navigationView = findViewById<NavigationView>(R.id.navigationView)
buttonDrawerToggle.setOnClickListener() {
drawerLayout.open()
}
val user = FirebaseAuth.getInstance().currentUser
val userEmail: String = user!!.email.toString()
email_textview.text = userEmail
}
}
这是抽屉标题:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="220dp"
android:gravity="bottom"
android:background="#F8D5A1"
android:padding="20dp"
>
<androidx.cardview.widget.CardView
android:layout_width="80dp"
android:layout_height="80dp"
app:cardCornerRadius="40dp"
app:cardElevation="0dp"
android:backgroundTint="#FFC107">
<ImageView
android:id="@+id/user_image"
android:contentDescription="NULL"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/person"/>
</androidx.cardview.widget.CardView>
<TextView
android:id="@+id/text_email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="@string/email"
android:textColor="@color/black"
android:textStyle="bold"
android:textSize="15sp"/>
</LinearLayout>
希望有人能帮助我
而不是这一行
var email_textview = findViewById<TextView>(R.id.text_email)
直接从 Binding 访问 TextView 试试这个
binding.textEmail