视图无效



我正在开发使用MVP Arch的应用程序。其中一个部分是3个片段,上面有一些文本视图和EditTexts。但是,有时我会在第二个片段中获得无效的视图(尤其是文本视图(。我将INIT代码放入viewCreated/onAtivitive created中,但它仍然不起作用。

这是我的XML

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true"
        android:background="@color/colorBackground">
<androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    <LinearLayout
            android:id="@+id/ll_header"
            app:layout_constraintTop_toTopOf="parent"
            android:gravity="center"
            android:background="@color/colorPrimaryLight"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
        <androidx.appcompat.widget.AppCompatTextView
                android:id="@+id/tv_initialName"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/profile_title_margin"
                android:layout_marginBottom="@dimen/profile_title_margin"
                android:textColor="@android:color/white"
                android:textSize="@dimen/profile_title_textSize"
                android:paddingTop="@dimen/profile_title_padding_topBottom"
                android:paddingBottom="@dimen/profile_title_padding_topBottom"
                android:paddingRight="@dimen/profile_title_padding_leftRight"
                android:paddingLeft="@dimen/profile_title_padding_leftRight"
                android:background="@drawable/bg_profiletitlename"
        />
    </LinearLayout>
    <androidx.recyclerview.widget.RecyclerView
            app:layout_constraintTop_toBottomOf="@id/ll_header"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
</androidx.constraintlayout.widget.ConstraintLayout>

我的片段(顺便说一句使用koin注射(:

 private val presenter:ProfilePresenter by inject{ parametersOf(this) }
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
    return inflater.inflate(R.layout.fragment_profile,container,false)
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    presenter.initNames()
}
override fun setUserInitialChar(initialChar: Char) {
    tv_initialName.text = initialChar.toString()
}

我的主持人:

override fun initNames() {
    view.setUserInitialChar(AppPreferences.customer.username.capitalize().first())
}

这给了我错误的错误,即我的 tv_initialname 是空的,但是如果该应用首次启动并显示该片段,则可以显示该视图。问题只有在我尝试使用" fragmentTransaction.replace"的片段时才会发生。

我认为在 onviewCreated 中的视图不能为null。我在这里做错了什么?

我想我会在这里回答以备将来参考。与碎片生命周期无关。我的问题是因为注射了Koin。我为演示者使用单{} ,因此它只会创建一次使用视图。(如您所见,我通过视图参数传递(我将其更改为 Factory {} ,现在正常工作。

最新更新