0

J'essaie d'utiliser la liaison de données dans l'Android. Mais finissez par obtenir l'erreur StackOverflow.méthode getter lancer StackOverflowError

loginViewModel.kt

class loginViewModel(): BaseObservable() { 

    @Bindable 
    var errorEmail:String?=null 
     get() { 
      if (userEmailAddress.isNullOrBlank()) 
       return "Please Enter the Email Address" 
      else if (!isValidEmail(userEmailAddress)) 
       return "Enter Valid Email Id." 
      else 
       return null 
     } 

    var userEmailAddress:String= String() 
     set(userEmailAddress){ 
      field=userEmailAddress 
      notifyPropertyChanged(R.id.email_address) 
      /*to check Email for validation on every character inserted by user*/ 
      notifyPropertyChanged(BR.errorEmail) 

     } 
     get() { 
     return userEmailAddress 
    } 



} 

Erreur journal:

07-04 13:54:15.435 6865-6865/com.example.itstym.reminder D/AndroidRuntime: Shutting down VM 
07-04 13:54:15.513 6865-6865/com.example.itstym.reminder E/AndroidRuntime: FATAL EXCEPTION: main 
                      Process: com.example.itstym.reminder, PID: 6865 
                      java.lang.StackOverflowError: stack size 8MB 
                       at com.example.itstym.reminder.loginViewModel.getUserEmailAddress(loginViewModel.kt:49) 
                       at com.example.itstym.reminder.loginViewModel.getUserEmailAddress(loginViewModel.kt:49) 

Je sais que l'erreur StackOverflow se produit quand il y a un appel récursif mais je ne suis pas en mesure de comprendre comment résoudre cette erreur?

<?xml version="1.0" encoding="utf-8"?> 
<layout 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"> 

    <data class="ActivityMainBinding"> 
     <variable 
      name="login" 
      type="com.example.itstym.reminder.loginViewModel" /> 
    </data> 

<android.support.constraint.ConstraintLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.example.itstym.reminder.MainActivity"> 


    <EditText 
     app:error="@{login.errorEmail}" 
     android:text="@{login.userEmailAddress}" 
     android:hint="Email Address" 
     android:id="@+id/email_address" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_marginEnd="8dp" 
     android:layout_marginStart="8dp" 
     android:ems="10" 
     android:inputType="textPersonName" 
     app:layout_constraintEnd_toEndOf="parent" 
     app:layout_constraintStart_toStartOf="parent" 
     app:layout_constraintTop_toTopOf="parent" 
     android:layout_marginTop="24dp"/> 


</layout> 

Répondre

0

Vous devez utiliser field dans votre getter ainsi, sinon il récursivement appeler:

var userEmailAddress: String = String() 
    set(userEmailAddress){ 
     field = userEmailAddress 
     ... 
    } 
    get() { 
     return field // <-- here 
    } 
1

Vous appelez récursive le getter. Vous devriez écrire

get() = field 

au lieu de

get() { 
    return userEmailAddress 
} 

L'identifiant field est utilisé dans les accesseurs de la propriété pour donner accès à l'backing field, en se référant à userEmailAddress finit par appeler la méthode getUserEmailAddress() qui conduit à l'infini boucle et StackOverflowError.