11

This this code : in which i open bottomSheetDialogFragment in that i want to add fragment.

I want to add multiple fragments in bottomsheetDialogFragment but it throws

java.lang.IllegalArgumentException: No view found for id 0x7f0a01cb

class AddNotesBottomSheetDialog : BottomSheetDialogFragment() {

private lateinit var bottomSheetDialog: BottomSheetDialog
private lateinit var bottomSheetBehavior: BottomSheetBehavior<View>

override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
    Log.v(LOG_TAG, "-> onCreateDialog")

    bottomSheetDialog = BottomSheetDialog(context!!)

    var view = View.inflate(context, R.layout.bottom_sheet_notes, null)
    bindViews(view)
    bottomSheetDialog.setContentView(view)
    bottomSheetBehavior = BottomSheetBehavior.from(view.parent as View)
    bottomSheetBehavior.isHideable = false
    bottomSheetBehavior.state = BottomSheetBehavior.STATE_EXPANDED

    return bottomSheetDialog
}

private fun bindViews(view: View) {
    loadAddNotesFragments()

}

override fun onStart() {
    super.onStart()
    Log.v(LOG_TAG, "-> onStart")

    bottomSheetBehavior.isHideable = false
    bottomSheetBehavior.state = BottomSheetBehavior.STATE_EXPANDED
    if (!visible)
        dialog.hide()
}


fun show(fragmentManager: FragmentManager) {
    Log.v(LOG_TAG, "-> show")

    visible = true
    if (isAdded) {
        Log.v(LOG_TAG, "-> Is already added")
        dialog.show()
    } else {
        Log.v(LOG_TAG, "-> Not added")
        show(fragmentManager, AddNotesBottomSheetDialog.LOG_TAG)
    }
}

override fun onDestroyView() {
    super.onDestroyView()
    Log.v(LOG_TAG, "-> onDestroyView")
}

private fun loadAddNotesFragments() {

    val createNoteFragment = CreateNoteFragment()
    val ft = fragmentManager?.beginTransaction()
    ft?.replace(R.id.placeHolderBottomSheet, createNoteFragment)
    ft?.commit()
}
}

Solved: I tried to add multiple fragments transaction in bottomSheetDialogFragment but it's not possible to do transaction in BottomSheetDialogFragment thats why its through this exception. so i used viewPager inside the BottomsheetDialog and its work perfect.

khushbu
  • 382
  • 1
  • 3
  • 15
  • Inside a `Fragment`, `fragmentManager` is the hosting `Activity`'s `FragmentManager`. You need to use the child `FragmentManager` to transact `Fragment`s inside another. – Mike M. Dec 17 '18 at 07:10
  • Call `loadAddNotesFragments()` after `setContentView()` inside your **onCreateDialog** method. – Jeel Vankhede Dec 17 '18 at 07:13
  • @JeelVankhede its not working .i tried both the solution. – khushbu Dec 17 '18 at 07:37
  • 1
    This is a fragment inside another fragment so,you need to use getChildFragmentManager(). Have a look at: https://stackoverflow.com/questions/6672066/fragment-inside-fragment – Sinan Ceylan Mar 16 '19 at 18:23
  • i needed to add fragments inside bottomsheetDialogFrament so this is not working but i used viewpager using getChildFragmentManager() and its working now..Thank you..@Sinan Ceylan for the idea :) – khushbu Mar 28 '19 at 09:19

1 Answers1

11

try calling loadAddNotesFragments() in

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
     loadAddNotesFragments()
}

And try using childFragmentManager to begin transaction's: reference

private fun loadAddNotesFragments() {

    val createNoteFragment = CreateNoteFragment()
    val ft = childFragmentManager()?.beginTransaction()
    ft?.replace(R.id.placeHolderBottomSheet, createNoteFragment)
    ft?.commit()
}

I believe this will solve your problem.

UPDATE:

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        return inflater.inflate(R.layout.bottom_sheet_notes, container, false)
    }

use this to inflate the content of bottomSheet, and

REMOVE:

override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
    Log.v(LOG_TAG, "-> onCreateDialog")

    bottomSheetDialog = BottomSheetDialog(context!!)

    var view = View.inflate(context, R.layout.bottom_sheet_notes, null)
    bindViews(view)
    bottomSheetDialog.setContentView(view)
    bottomSheetBehavior = BottomSheetBehavior.from(view.parent as View)
    bottomSheetBehavior.isHideable = false
    bottomSheetBehavior.state = BottomSheetBehavior.STATE_EXPANDED

    return bottomSheetDialog
}

ADD:

 override fun onStart() {
        super.onStart()
        val bottomSheet = dialog.findViewById(android.support.design.R.id.design_bottom_sheet) as ViewGroup   //FrameLayout as my 
        val mBehavior = BottomSheetBehavior.from(bottomSheet)

        //Add Behavior logic here
    }

NOTE: No need of overriding onCreateDialog() unless you want your own Dialog to be initiated, i.e some other type of dialog.

Anmol
  • 8,110
  • 9
  • 38
  • 63
  • 1
    Thank you for help but this is not working.Same exception was thrown – khushbu Dec 17 '18 at 07:52
  • while debugging can you check in which line this exception is triggered ! java.lang.IllegalArgumentException: No view found for id 0x7f0a01cb @khushbu – Anmol Dec 17 '18 at 08:38
  • if you remove `onCreateDialog()` then `getDialog()` in `onStart()` will return `null`, as the dialog has not been created. So, there will be a `NullPointerException` in the code you suggested. – Alexey Timokhin Aug 26 '19 at 07:56
  • No, it will not give NPE as `BottomSheetDialogFragment` has an implementation of `onCreateDialog` we should override it.It should be overridden only if you want to create custom dialog. @AlexeyTimokhin Can u tell me in what case u are getting NPE? – Anmol Aug 26 '19 at 09:33
  • @Anmol Yes, you are right. My mistake was that I was adjusting the `BottomSheetBehaviour` in `onCreateView()` and there `getDialog()` returned `null`. In `onStart()` it's not `null`, so I moved the adjustments there and it worked fine. Thank you! – Alexey Timokhin Aug 26 '19 at 12:20
  • This works for me. The main point is the childFragmentManager.beginTransaction(). You can find the code here https://github.com/jiahaoliuliu/SimpleLayoutTransitions – jiahao Aug 06 '20 at 12:24