【问题标题】:No view found for id (...) for fragment inside Custom View在自定义视图中找不到片段的 id (...) 视图
【发布时间】:2018-06-17 03:15:37
【问题描述】:

我创建了接受片段的自定义视图。所以,后来我的自定义视图有动态内容(片段)。但是添加片段时出现以下错误。

原因:java.lang.IllegalArgumentException:没有为片段 SampleFragment{470c924 #0 id=0x7f080058} 找到 id 0x7f080058 (android.amanah.com.amanah:id/flContent) 的视图

em>

这是我的自定义视图:

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()
        }
...

我的 xml 在我的自定义视图 (layout_bottom_sheet.xml) 布局中作为片段容器,如下所示:

...
<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

【问题讨论】:

    标签: android android-fragments kotlin android-custom-view


    【解决方案1】:

    Dialog与windowmanager分开显示,所以不包含在activity的view层中。所以找不到你的 flContent。

    建议您使用BottomSheepDialogFragment,并将片段替换为childFragmentManager。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-05
      • 2013-09-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-23
      • 1970-01-01
      相关资源
      最近更新 更多