【问题标题】:How to access a fragment that is defined in a different view?如何访问在不同视图中定义的片段?
【发布时间】:2019-09-19 15:00:30
【问题描述】:

我有一个在activity_main.xml 中定义的片段。我需要在另一个使用不同视图的类中引用它。我在activity_main.xml 中定义了片段,因为我需要它是持久的并附加到主屏幕的底部。否则我会在需要引用它的视图中定义它。

我尝试在课堂上定义主要活动并使用supportFragmentManager.findFragmentById(R.id.bottom_sheet_view),但一直返回错误:

null cannot be cast to non-null type com.ds.base.fragments.BottomTimeSheet
        at com.ds.base.fragments.SessionTimeFragment.onCreateVie

这是我如何尝试在类中实例化片段的 sn-p:

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        val view = inflater.inflate(R.layout.fragment_session_time,container, false)

        val main = activity as MainActivity
        var frag = main.supportFragmentManager.findFragmentById(R.id.bottom_sheet_view) as BottomTimeSheet

        if(frag == null){
            frag = BottomTimeSheet()
        }
        bottomTimeSheet = frag
        .
        .
        .
        .

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 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"
    tools:context=".MainActivity">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/constraintLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <fragment
            android:id="@+id/nav_host_fragment"
            android:name="androidx.navigation.fragment.NavHostFragment"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            app:defaultNavHost="true"
            app:layout_constraintBottom_toTopOf="@+id/bottom_navigation_view"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:navGraph="@navigation/base_nav_graph"
            />

        <com.google.android.material.button.MaterialButton
            android:id="@+id/bay_number_status"
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:background="@color/white_regular"
            android:fontFamily="@font/parkinson_medium"
            android:text="---&#10;Bay #"
            android:textColor="@color/black_regular"
            android:textSize="15sp"
            app:itemBackground="@color/white_regular"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintHeight_percent=".1"
            app:layout_constraintHorizontal_bias="1.0"
            app:layout_constraintRight_toRightOf="parent"
            style="@style/Widget.MaterialComponents.Button.TextButton"
            />

        <fragment
            android:id="@+id/bottom_navigation_view"
            android:name="com.ds.base.fragments.BottomNavigationFragment"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            tools:layout="@layout/fragment_bottom_navigation"
            />

        <fragment
            android:id="@+id/bottom_sheet_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:name="com.ds.base.fragments.BottomTimeSheet"
            app:layout_constraintTop_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            tools:layout="@layout/bottom_sheet"/>


    </androidx.constraintlayout.widget.ConstraintLayout>

</androidx.coordinatorlayout.widget.CoordinatorLayout>

【问题讨论】:

    标签: xml android-studio android-fragments kotlin


    【解决方案1】:

    您在活动生命周期中过早地搜索片段。 Fragment 的onCreateView() 在Activity 的onCreate() 完成之前被调用。将尝试获取对另一个片段的引用的代码移动到onActivityCreated()

    【讨论】:

      【解决方案2】:

      正如 Tenfour04 所说,您应该在 onActivityCreated 或 onViewCreated 创建当前片段后访问视图。或者您也可以为您的片段设置一个标签,然后尝试 getFragmentByTag。 希望对您有所帮助。

      【讨论】:

        猜你喜欢
        • 2013-12-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-11-26
        • 1970-01-01
        • 1970-01-01
        • 2020-04-01
        • 1970-01-01
        相关资源
        最近更新 更多