Ksh
Ksh

Reputation: 11

CreateView on button click error - IllegalArgumentException: No view found for id

I'm trying to launch another fragment from current fragment on button click. But I see an exception thrown while launching the new fragment.

Exception:

EXCEPTION: main
    Process: com..., PID: 6047
    java.lang.IllegalArgumentException: No view found for id 0x7f0a01bf (com...:id/tile_providers_root_layout) for fragment TileProviderFragment{850b9b6} (4d3783b0-443c-4b70-b24b-dcda5a49e400) id=0x7f0a01bf TileProviderFragment}
        at android.support.v4.app.FragmentStateManager.createView(FragmentStateManager.java:483)
...

Fragment tiles_provider_fragment.xml file:

<?xml version="1.0" encoding="utf-8"?>

<FrameLayout
    android:id="@+id/tile_providers_root_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="?attr/colorPrimary">

  <android.support.v7.widget.RecyclerView
      android:id="@+id/tile_providers"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:layout_marginTop="16dp"
      tools:listitem="@layout/tile_provider_item"
      android:paddingTop="1dp"/>

</FrameLayout>

Code to launch the fragment:

    val tileProviderFragment: TileProviderFragment = TileProviderFragment.createInstance()
    activity?.supportFragmentManager?.beginTransaction()
      ?.replace(R.id.tile_providers_root_layout, tileProviderFragment, "TileProviderFragment")
      ?.addToBackStack(null)
      ?.commit()

OnCreateView of target fragment:

  override fun onCreateView(
    layoutInflater: LayoutInflater, viewGroup: ViewGroup?, bundle: Bundle?): View? {
    super.onCreateView(layoutInflater, viewGroup, bundle)

    val view = layoutInflater.inflate(R.layout.tiles_provider_fragment, viewGroup, false)
}

Thanks!

Upvotes: 0

Views: 547

Answers (1)

ianhanniballake
ianhanniballake

Reputation: 199860

When you use activity?.supportFragmentManager, you're using the Activity's FragmentManager which means the R.id you pass in needs to be part of the Activity's layout.

If you're referencing part of a Fragment's layout, then you should be using childFragmentManager to make the fragment a child fragment, fully contained in the parent fragment.

Upvotes: 2

Related Questions