【发布时间】: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="--- 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