Android 在自定义视图中找不到片段id(…)的视图
我创建了接受片段的自定义视图。因此,稍后我的自定义视图将具有动态内容(片段)。但我在添加片段时遇到以下错误 原因:java.lang.IllegalArgumentException:未找到片段SampleFragment{470c924#0 id=0x7f080058}的id 0x7f080058(android.amanah.com.amanah:id/flContent)的视图 以下是我的自定义视图:Android 在自定义视图中找不到片段id(…)的视图,android,android-fragments,kotlin,android-custom-view,Android,Android Fragments,Kotlin,Android Custom View,我创建了接受片段的自定义视图。因此,稍后我的自定义视图将具有动态内容(片段)。但我在添加片段时遇到以下错误 原因:java.lang.IllegalArgumentException:未找到片段SampleFragment{470c924#0 id=0x7f080058}的id 0x7f080058(android.amanah.com.amanah:id/flContent)的视图 以下是我的自定义视图: class AmBottomSheet(context: Context, attrib
class AmBottomSheet(context: Context, attributeSet: AttributeSet?) : ConstraintLayout(context, attributeSet) {
private val state = State()
init {
LayoutInflater.from(context)
.inflate(R.layout.layout_bottom_sheet, this, true)
}
fun bind(newState: State.() -> Unit) {
state.apply(newState)
render(state)
}
private fun render(state: State) {
tvTitle.text = state.title
if (state.subTitle != null) {
tvSubTitle.text = state.subTitle
} else {
tvSubTitle.visibility = GONE
}
ivClose.setOnClickListener {
state.closeClickListener?.invoke(it)
}
attachContentFragment()
}
private fun attachContentFragment() {
val transaction = state.supportFragmentManager?.beginTransaction()
transaction?.replace(R.id.flContent, state.layoutContent)
transaction?.commit()
}
class State {
var title: String? = null
var subTitle: String? = null
var closeClickListener: (View?.() -> Unit)? = null
var supportFragmentManager: FragmentManager? = null
var layoutContent: Fragment? = null
}
}
我这样称我的自定义视图:
...
bottomSheetView.bind {
title = "Informasi"
subTitle = "We can even add some listeners to the BottomSheet and for example do something when the dialog is dismissed"
closeClickListener = {
bottomSheetDialog.dismiss()
}
supportFragmentManager = getSupportFragmentManager()
layoutContent = SampleFragment.newInstance()
}
...
...
<FrameLayout
android:id="@+id/flContent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/standard_margin_x2"
android:background="@color/colorSlate"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tvSubTitle"
app:layout_constraintVertical_bias="0.0" />
...
以及自定义视图(layout\u bottom\u sheet.xml)中作为片段容器的xml布局,如下所示:
...
bottomSheetView.bind {
title = "Informasi"
subTitle = "We can even add some listeners to the BottomSheet and for example do something when the dialog is dismissed"
closeClickListener = {
bottomSheetDialog.dismiss()
}
supportFragmentManager = getSupportFragmentManager()
layoutContent = SampleFragment.newInstance()
}
...
...
<FrameLayout
android:id="@+id/flContent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/standard_margin_x2"
android:background="@color/colorSlate"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tvSubTitle"
app:layout_constraintVertical_bias="0.0" />
...
。。。
...
为什么我的自定义视图找不到flContent?该对话框与windowmanager分开显示,因此它不包括在活动的视图层中。所以找不到你的内容 建议您使用BottomSheepDialogFragment并用childFragmentManager替换该片段