如何访问在其他视图中定义的片段?

时间:2019-09-19 15:00:30

标签: xml android-studio android-fragments kotlin

我有一个在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

以下是我如何实例化类内部片段的摘要:

    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>

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:1)

您正在活动生命周期中过早搜索片段。在活动的[JsonObject] public class Package { [JsonProperty("Targets")] public List<Target> Targets { get; set; } } public class Sharpmake { [JsonProperty("Name")] public string Name { get; set; } [JsonProperty("Excludes")] public IList<string> Excludes { get; set; } [JsonProperty("Dest")] public string Dest { get; set; } [JsonProperty("Includes")] public IList<string> Includes { get; set; } } public class File { [JsonProperty("Source")] public string Source { get; set; } [JsonProperty("Dest")] public string Dest { get; set; } } public class Directory { [JsonProperty("Source")] public string Source { get; set; } [JsonProperty("Dest")] public string Dest { get; set; } [JsonProperty("Includes")] public IList<string> Includes { get; set; } } public class Target { [JsonProperty("Name")] public string Name { get; set; } [JsonProperty("Sharpmakes")] public IList<Sharpmake> Sharpmakes { get; set; } [JsonProperty("Includes")] public IList<string> Includes { get; set; } [JsonProperty("Files")] public IList<File> Files { get; set; } [JsonProperty("Directories")] public IList<Directory> Directories { get; set; } } public class RootObject { [JsonProperty("Targets")] public IList<Target> Targets { get; set; } } 完成之前调用该片段的onCreateView()。将试图获取对另一个片段的引用的代码移到onCreate()中。