簡體   English   中英

IllegalStateException textview使用Kotlin不能為null

[英]IllegalStateException textview cant be null with Kotlin

我試圖通過在導航抽屜中設置TextView的文本來創建一種在活動開始時更新UI的方法。 我不斷收到錯誤消息,TextView不能為null?

我收到的錯誤是非法狀態異常userEmail不能為null

這是updateUI,用於更新導航抽屜中的配置文件信息

private fun updateUI(user:FirebaseUser?) {
   if (user != null) {
   val userEmailText = findViewById<TextView>(R.id.userEmail)
   userEmail.text = user.email
   val errorTriage : String? = user.email
   println("User Email is ="+errorTriage)
   } else {
val userEmailText: TextView = findViewById(R.id.userEmail)

   userEmailText.text = "Johndoe@someemail.com"
   userNameText.text = "John Doe"

   }}

如果用戶已登錄,則這是對UpdateUI的調用(如果已登錄),則更新導航抽屜中的配置文件數據。 如果沒有,它將啟動FirebaseUI登錄活動。

override fun onStart() {
    super.onStart()
    //Checks to see if user is already signed in
    if (mAuth.currentUser != null) {
        updateUI(mAuth.currentUser)
   } else {
        //user is not signed in
        startActivityForResult(
  AuthUI.getInstance().createSignInIntentBuilder().build(),
                RC_SIGN_IN)
   }}

這是布局

 <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
android:paddingTop="@dimen/nav_header_vertical_spacing"
        app:srcCompat="@mipmap/ic_launcher_round" />

    <TextView
        android:id="@+id/userName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
     android:paddingTop="@dimen/nav_header_vertical_spacing"
        android:text="Android Studio"
        android:textAppearance="@style/TextAppearance.AppCompat.Body1" />
  <TextView
        android:id="@+id/userEmail"
        android:layout_width="wrap_content"

抱歉,我整夜都在工作,並發布了生氣的代碼lol。 我將代碼更新為問題所在。 我也意識到我將值命名為userEmailText並試圖設置

userEmail.text = user.email

代替

userEmailText.text = user.email

那可以解釋為什么我執行println時會正確顯示log cat

更新! 因此,我發現它在首次安裝和登錄時就可以正常工作,但是由於某些原因,如果在首次登錄后在打開應用程序時關閉該應用程序,則該應用程序會在updateUI部分崩潰。 如果用戶已經登錄,它無法更新,那我是否在漫游它與它有關。 我可能會嘗試使用共享的首選項,並保存用戶名和電子郵件,然后在注銷時將其擦除。

編輯這是一個很久以前的問題,但是它是什么,我討厭我隨后需要從onCreate和onStart調用的正確變量。 在問這個問題時,我正在努力地工作,只是編碼的想法,更不用說Android生命周期的概念了。 在導航標題中用於初始化視圖的方式也存在一個已知問題。 初始化導航抽屜視圖之后,便可以初始化導航抽屜標題,然后可以在其中初始化文本視圖。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM