简体   繁体   中英

No view found for id (…) for fragment inside Custom View

i create custom view that accept fragment. So, later my custom view have dynamic content ( Fragment ). But i got this following error when adding fragment.

Caused by: java.lang.IllegalArgumentException: No view found for id 0x7f080058 (android.amanah.com.amanah:id/flContent) for fragment SampleFragment{470c924 #0 id=0x7f080058}

Here is my custom view :

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

I call my custom view like this :

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

And my xml as fragment container in my custom view ( layout_bottom_sheet.xml ) layout like this :

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

Why my custom view cannot find flContent ?

The Dialog is displayed separately from the windowmanager, so it is not included in the view layer of the activity. So can't find your flContent.

It is recommended that you use BottomSheepDialogFragment and replace the fragment with the childFragmentManager.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM