- In the
onCreateView()
method of a fragment, I am trying to initialize a CustomView with behavior similar to a fab button. - Upon clicking on this custom view an animation is supposed to run which utilizes an empty
RelativeLayout
which is present in the parentViewGroup
in the Fragment. - I am passing this
ViewGroup
to the CustomView in a constructor. - When I try to create a reference to the empty
RelativeLayout
by callingViewGroup.findViewById(R.id.target)
, the reference create holds null ultimately causing aNullPointerException
upon animation - However, if I use
ViewGroup.getChildAt(1)
to create the reference, the animation works just fine. - By using watchers, I found that the id of the
ViewGroup.getChildAt(1)
is different from the R.id.target which has been assigned to it via xml. - What sorcery is this?
- I have browsed already for a few days to be able to find a question which describes this situation.
Extra Information:
- We recently switched from support libraries to androidX.
- fragment.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/top_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout>
<!-- Other views -->
</LinearLayout>
<!-- This is the layout i am trying to instantiate -->
<RelativeLayout
android:id="@+id/target"/>
</RelativeLayout>