0
  • 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 parent ViewGroup 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 calling ViewGroup.findViewById(R.id.target), the reference create holds null ultimately causing a NullPointerException 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:

  1. We recently switched from support libraries to androidX.
  2. 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>
Akshay Choudhry
  • 122
  • 1
  • 1
  • 9
  • try to do all the stuff in onViewCreated() of fragment, in this there is an argument which returns rootview, it is always best practice to not play with views in onCreateView(), but onViewCreated(), check once and let me know – akashzincle Apr 14 '20 at 10:03
  • why don't you try in `onViewCreated` method? – Parth Apr 14 '20 at 10:03
  • please rename your id `RelativeLayout` with some other name and let me know it is working or not – Priyanka Apr 14 '20 at 10:04
  • @akashzincle I have tried to do that and unfortunately the issue persists. – Akshay Choudhry Apr 14 '20 at 10:16
  • @Priyankagb that name was chosen for sake of simplicity, the real id of the layout is something else. – Akshay Choudhry Apr 14 '20 at 10:17
  • I think there is some issue with item index in viewgroup, check viewgroup.getchildat(0) , viewgroup.getchildat(1) may be you are mixing it with some other view, and this your relativelayout is at some other index in viewgroup, try to check childcount of viewgroup, and check every child at all the indexes – akashzincle Apr 14 '20 at 10:24
  • @akashzincle Getting the view by viewgroup.getchildat(1) is not the ideal way to do it. The problem is that ViewGroup.findViewById(R.id.RelativeLayout) is NOT working and would be the ideal way to do this. – Akshay Choudhry Apr 14 '20 at 10:28
  • Yes @AkshayChoudhry you are right, It's not the ideal way, I am just asking to debug the viewgroup and all of its children hierarchy using these methods for testing purpose, Can you post XML of your fragment here? – akashzincle Apr 14 '20 at 10:42
  • @akashzincle I have added the skeleton of the xml that i am using for the fragment. I would like to restate to you that viewgroup.getchildat(1) **works perfectly** but that's not the solution i think i should use. – Akshay Choudhry Apr 14 '20 at 10:51
  • 1
    Yes completely Agreed, Accessing view by their position is always bad, always access them by ids. I found a similar kind of issue on stackoverflow, check https://stackoverflow.com/a/3264647/3497972 – akashzincle Apr 14 '20 at 10:57
  • 1
    Check this also https://stackoverflow.com/a/9651509/3497972 – akashzincle Apr 14 '20 at 11:01
  • Thanks for those resources but unfortunately I have gone over them for a second time but I was unable to get it to work. These callbacks are only a few milliseconds of difference. Even if I try to create a reference to the existing view on the click of a button which would be at least 1000ms later, I still get a different id in the debugger. – Akshay Choudhry Apr 14 '20 at 11:18

0 Answers0