Lateinit属性mScrollView尚未初始化

时间:2018-12-20 15:14:06

标签: android kotlin

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

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"/>

你知道为什么吗?

3 个答案:

答案 0 :(得分:2)

onCreateView()中初始化mScrollView

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

答案 1 :(得分:1)

在Kotlin中,您还可以使用Kotlin扩展来进行视图绑定。按照此link

在gradle中配置依赖项

然后,使用import kotlinx.android.synthetic.main.<layout>.*导入布局视图ID 您将摆脱Butterknife,并且始终将视图绑定到代码中,而无需任何findviewbyids或@BindViews注释

答案 2 :(得分:0)

我建议使用Kotlin摆脱掉Android中的ButterKnife,因为它具有内置的synthetic功能。 您可以在代码中直接使用名称为scroll_view的视图:

private fun setupLayout() {
    scroll_view.viewTreeObserver.addOnGlobalLayoutListener {
        val r = Rect()
        scroll_view.getWindowVisibleDisplayFrame(r)
        val screenHeight = scroll_view.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 = scroll_view.scrollY
            val sh = scroll_view.height
            val delta = bottom - (sy + sh)
            scroll_view.smoothScrollBy(0, delta)
        }
    }
}

原因scroll_view不是Java / Kotlin名称约定,因此您可以将XML重命名为scrollView之类的东西,并且根本不需要类变量。 您只需要导入import kotlinx.android.synthetic.main.main_activity.*之类的东西,但是IDE会自动生成它。