-1

I'm working on an app where I have to implement the onClick method on the fragment. When a button is clicked I want to open another fragment, but I'm facing an error with fragment id.

I used fragment manager and fragment transaction in the onClick method like below:

override fun onClick(v: View?) {
val fm = childFragmentManager
val ft = fm.beginTransaction()
ft.add(R.layout.fragment_extended_subscribers, ExtendedSubscribersFragment())
ft.addToBackStack(null)
ft.commit()

I also tried to add an id to FrameLayout and add it to fragment transaction but still the same error:

ft.add(R.id.fragment_for_subs,ExtendedSubscribersFragment())

The logcat error is:

java.lang.IllegalArgumentException: No view found for id 0x7f0d0037 (al.mgr.ekranet:layout/fragment_extended_subscribers) for fragment ExtendedSubscribersFragment{d6186df #0 id=0x7f0d0037}

Here is my onCreateView function:

override fun onCreateView(
    inflater: LayoutInflater, container: ViewGroup?,
    savedInstanceState: Bundle?
): View? {
    // Inflate the layout for this fragment
    val view =  inflater.inflate(R.layout.fragment_extended_subscribers, container, false)
    getAllSubscribers()
    return view

Here is the layout associated with the fragment.

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/fragment_for_subs"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".homeFragments.ExtendedSubscribersFragment">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/all_subscribers_list"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</FrameLayout>
Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98
Erald Developer
  • 359
  • 1
  • 6
  • 16

1 Answers1

-1

try this:

    override fun onClick(v:View?) {
        // Change this to
        //-----------------------
//        val fm = childFragmentManager
        // this
        //-----------------------
        val fm = fragmentManager

        val ft = fm.beginTransaction()
        // Change: layout -> id
        ft.add(R.id.fragment_for_subs, ExtendedSubscribersFragment())
        ft.addToBackStack(null)
        ft.commit()
    }

For more info: link

MojoJojo
  • 783
  • 5
  • 15