我不明白为什么会有这种错误:

kotlin.UninitializedPropertyAccessException: lateinit property mScrollView has not been initialized

我的意思是我有这种代码:
@BindView(R.id.scroll_view)
lateinit var mScrollView: ScrollView

在这里您可以找到onCreateView
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
    AndroidSupportInjection.inject(this)
    val view = inflater.inflate(R.layout.fragment_delete_account_reason, container, false)

    mUnbinder = ButterKnife.bind(this, view)
    ViewUtils.setupUI(view, activity)
    hideNextBtnOnKeyboardOpens(mScrollView, mNextBtn)

    setupLayout()
    mPresenter.getReasonKeyOfDeleteAccount()

    return view
}

private fun setupLayout() {
    mScrollView.viewTreeObserver.addOnGlobalLayoutListener {
        val r = Rect()
        mScrollView.getWindowVisibleDisplayFrame(r)
        val screenHeight = mScrollView.rootView.height
        val keypadHeight = screenHeight - r.bottom

        if (keypadHeight > screenHeight * 0.15 && mInput.hasFocus()) { // Keyboard is visible
            val bottom = mNbCharactersMax.getBottom() + mNbCharactersMax.getPaddingBottom()
            val sy = mScrollView.scrollY
            val sh = mScrollView.height
            val delta = bottom - (sy + sh)
            mScrollView.smoothScrollBy(0, delta)
        }
    }
}

您可以在这里找到我的xml。
所有其他ID工作(每个ID都匹配),但我对scrollView只是有一个问题,我不知道为什么。如果删除ScrollView,一切都会正常,但是我需要这样做。
<android.support.constraint.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:layout_width="match_parent"
android:layout_height="match_parent">

<ScrollView
    android:id="@+id/scroll_view"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:animateLayoutChanges="true"
    android:layout_marginBottom="@dimen/general_padding"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintBottom_toTopOf="@id/delete_account_next"
    android:fillViewport="true">

    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:id="@+id/delete_account_description"
            fontPath="fonts/SourceSansPro-SemiBold.ttf"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="@dimen/general_padding"
            android:layout_marginTop="@dimen/general_padding"
            android:layout_marginEnd="@dimen/general_padding"
            android:letterSpacing="@dimen/general_letter_spacing"
            android:text="@string/delete_account_reason_description"
            android:textColor="@color/vision"
            android:textSize="18dp"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            tools:ignore="MissingPrefix,SpUsage" />

        <RadioGroup
            android:id="@+id/delete_account_radio_group"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:layout_marginEnd="@dimen/two_third_padding"
            android:layout_marginStart="@dimen/two_third_padding"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/delete_account_description">

        </RadioGroup>

        <android.support.design.widget.TextInputLayout
            android:id="@+id/delete_account_input_reason_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/delete_account_reason_specify_hint"
            android:layout_marginStart="17dp"
            android:layout_marginEnd="17dp"
            android:layout_marginTop="@dimen/general_padding"
            android:visibility="invisible"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toBottomOf="@id/delete_account_radio_group">

            <android.support.v7.widget.AppCompatEditText
                android:id="@+id/delete_account_input_password"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                fontPath="fonts/SourceSansPro-SemiBold.ttf"
                tools:ignore="MissingPrefix,SpUsage"
                android:imeOptions="actionDone"
                android:textColor="@color/jiren"
                android:textSize="16dp" />

        </android.support.design.widget.TextInputLayout>

        <TextView
            android:id="@+id/delete_account_reason_nb_caracters"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            fontPath="fonts/SourceSansPro-SemiBold.ttf"
            app:layout_constraintTop_toBottomOf="@id/delete_account_input_reason_layout"
            app:layout_constraintEnd_toStartOf="@id/delete_account_reason_nb_caracters_max"
            android:layout_marginEnd="@dimen/quarter_padding"
            android:textColor="@color/wolverine"
            android:textSize="14dp"
            android:text="0"
            android:visibility="invisible"
            android:letterSpacing="@dimen/general_letter_spacing"
            tools:ignore="HardcodedText,MissingPrefix,SpUsage"/>

        <TextView
            android:id="@+id/delete_account_reason_nb_caracters_max"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            fontPath="fonts/SourceSansPro-SemiBold.ttf"
            app:layout_constraintTop_toBottomOf="@id/delete_account_input_reason_layout"
            app:layout_constraintEnd_toEndOf="parent"
            android:layout_marginEnd="@dimen/general_padding"
            android:text="@string/delete_account_reason_specify_max_characters"
            android:textColor="@color/wolverine"
            android:textSize="14dp"
            android:visibility="invisible"
            android:letterSpacing="@dimen/general_letter_spacing"
            tools:ignore="MissingPrefix,SpUsage"/>

    </android.support.constraint.ConstraintLayout>

</ScrollView>

<Button
    style="?borderlessButtonStyle"
    android:id="@+id/delete_account_next"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:minWidth="0dp"
    android:minHeight="0dp"
    android:textSize="17dp"
    android:paddingTop="@dimen/half_padding"
    android:paddingBottom="@dimen/half_padding"
    android:paddingStart="@dimen/one_half_padding"
    android:paddingEnd="@dimen/one_half_padding"
    android:textAllCaps="false"
    android:layout_marginTop="@dimen/double_padding"
    android:layout_marginBottom="@dimen/one_half_padding"
    android:textColor="@color/white"
    android:textAlignment="center"
    android:background="@drawable/button_blue"
    android:letterSpacing="@dimen/general_letter_spacing"
    android:text="@string/delete_account_next_label"
    android:enabled="false"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintBottom_toBottomOf="parent"
    fontPath="fonts/SourceSansPro-SemiBold.ttf"
    tools:ignore="MissingPrefix,SpUsage"/>

你明白为什么吗?

最佳答案

onCreateView()中初始化mScrollView:

mScrollView = view.findViewById<ScrollView>(R.id.scroll_view)
setupLayout()

关于android - Lateinit属性mScrollView尚未初始化,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53871354/

10-10 14:59