【问题标题】:getter method throwing StackOverflowError [duplicate]getter 方法抛出 StackOverflowError [重复]
【发布时间】:2017-07-04 08:36:58
【问题描述】:

我正在尝试在 Android 中使用数据绑定。但最终得到 StackOverflow 错误。

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
    }



}

错误日志:

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)

我知道有任何递归调用时会发生 StackOverflow 错误,但我不知道如何解决此错误?

<?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>

【问题讨论】:

    标签: android data-binding kotlin android-databinding 2-way-object-databinding


    【解决方案1】:

    您正在递归调用 getter。你应该写

    get() = field
    

    而不是

    get() {
        return userEmailAddress
    }
    

    field 标识符用于属性的访问器中以提供对backing field 的访问,而引用userEmailAddress 最终调用getUserEmailAddress() 方法导致无限循环和StackOverflowError。

    【讨论】:

      【解决方案2】:

      你也应该在你的getter中使用field,否则它会递归调用自己:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-10-28
        • 2012-04-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-01-12
        • 1970-01-01
        • 2018-07-10
        相关资源
        最近更新 更多