Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IllegalStateException getContentRoot(activity).getChildAt(0) returning null #61

Closed
beshoy-samy opened this issue Sep 2, 2020 · 6 comments
Labels

Comments

@beshoy-samy
Copy link

beshoy-samy commented Sep 2, 2020

Hi there, first of all, thank you for doing this great project I really like it

lately, I have got a crash reporting from Crashlytics

Crash caused by java.lang.IllegalStateException
getContentRoot(activity).getChildAt(0) must not be null
net.yslibrary.android.keyboardvisibilityevent.KeyboardVisibilityEvent.getActivityRoot$keyboardvisibilityevent_release (KeyboardVisibilityEvent.kt:147)

in my activity, I have a fragment tag inside a parent view group which is ConstraintLayout
I am using NavigationComponent from jetpack to navigate between my fragments

I hope this will help you fix this and I promise in my free time I will try to fix it by myself
Keep the good work you really did a nice job

@yshrsmz
Copy link
Owner

yshrsmz commented Sep 2, 2020

Duplicate of #59

Thanks for the report

@yshrsmz
Copy link
Owner

yshrsmz commented Sep 2, 2020

@beshoy-samy Could you add a sample layout and the code around the KeyboardVisibilityEvent setup?

@beshoy-samy
Copy link
Author

here in my fragment, I have Unregistrar variable which is nullable to be released in onDestroyView

private var keyboardEventRegister: Unregistrar? = null

in onViewCreated fragment lifecycle event, I initialize my views and my keyboard visibility event

keyboardEventRegister = KeyboardVisibilityEvent.registerEventListener(
            activity,
            object : KeyboardVisibilityEventListener {
                override fun onVisibilityChanged(isOpen: Boolean) {
                    //clearing focus from the search widget which depends on it to call searchCallback
                    if (!isOpen) binding.searchWidget.clearFocus()
                }
            })

and here is my fragment layout

<?xml version="1.0" encoding="utf-8"?><!--
  ~ Created by Beshoy Samy on 8/20/20 4:06 PM.
  ~ Copyright (c) 2020 . All rights reserved.
  ~ Last modified 8/20/20 4:06 PM.
  -->

<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">

    <data>

        <import type="android.view.View" />

        <variable
            name="hide"
            type="Boolean" />

    </data>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/parent_container"
        style="@style/AppTheme.ParentViewStyle.NoPadding"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".features.main.discover.DiscoverFragment">

        <FrameLayout
            android:id="@+id/home_bar_container"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/white"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">

            <androidx.constraintlayout.widget.ConstraintLayout
                android:id="@+id/home_bar"
                style="@style/AppTheme.PrimaryClickableView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:clickable="@{!hide}"
                android:clipToPadding="false"
                android:minHeight="?actionBarSize"
                android:paddingStart="0dp"
                android:paddingEnd="0dp">

                <com.google.android.material.textview.MaterialTextView
                    android:id="@+id/home_title"
                    style="@style/AppTheme.NaviStyleTitle"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/searching_in"
                    android:textSize="@dimen/text_normal"
                    app:layout_constraintBottom_toTopOf="@id/filter_state_tv"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent"
                    app:layout_constraintVertical_chainStyle="packed" />

                <com.google.android.material.textview.MaterialTextView
                    android:id="@+id/filter_state_tv"
                    style="@style/AppTheme.ListPrimaryTitleStyle"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:drawablePadding="@dimen/padding_tiny"
                    android:gravity="center_vertical"
                    android:text="@string/all_cities"
                    app:drawableEndCompat="@drawable/ic_expand_more"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toBottomOf="@id/home_title"
                    tools:text="Cairo, Zamalek" />
            </androidx.constraintlayout.widget.ConstraintLayout>

        </FrameLayout>

        <com.pinnavi.navi.features.widgets.search.SearchViewWidget
            android:id="@+id/search_widget"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/white"
            android:hint="@string/search"
            android:paddingStart="@dimen/padding_normal"
            android:paddingTop="@dimen/padding_small"
            android:paddingEnd="@dimen/padding_normal"
            android:paddingBottom="@dimen/padding_small"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/home_bar_container" />

        <com.pinnavi.navi.features.widgets.list_merchant_categories.HomeMerchantCategoriesWidget
            android:id="@+id/merchant_categories"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/white"
            android:paddingStart="@dimen/padding_normal"
            android:paddingTop="@dimen/padding_small"
            android:paddingEnd="@dimen/padding_normal"
            android:paddingBottom="@dimen/padding_small"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/search_widget"
            tools:listitem="@layout/item_list_merchant_category" />

        <FrameLayout
            android:id="@+id/merchants_state_container"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:animateLayoutChanges="true"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/merchant_categories">

            <com.pinnavi.navi.features.widgets.list_merchants.MerchantsListWidget
                android:id="@+id/merchants_widget"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginBottom="?actionBarSize"
                android:paddingTop="@dimen/padding_small"
                android:visibility="@{hide? View.INVISIBLE : View.VISIBLE}" />

        </FrameLayout>

        <androidx.constraintlayout.widget.Group
            android:id="@+id/first_state_group"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:visibility="@{hide? View.INVISIBLE : View.VISIBLE}"
            app:constraint_referenced_ids="search_widget,merchant_categories" />

    </androidx.constraintlayout.widget.ConstraintLayout>
</layout>

please hint that custom view named SearchViewWidget is a view which has an EditText child

@beshoy-samy
Copy link
Author

here is how I am removing the registry event

override fun onDestroyView() {
        locationHelper?.destroy()
        locationHelper = null
        keyboardEventRegister?.unregister()
        keyboardEventRegister = null
        super.onDestroyView()
    }

@emredirican
Copy link

@yshrsmz @beshoy-samy I worked around the issue with the code piece below while calling it on onViewCreated(). It is similar to calling a method onAttach(), just probably a better way considering the fact that it is deprecated. It might even be the correct way to fix it, since we are trying to access the functionality from a fragment anyway.

requireActivity().lifecycle.addObserver(object : LifecycleObserver {

      @OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
      fun onActivityCreated() {
        keyboardListenerUnregistrar = KeyboardVisibilityEvent.registerEventListener(
            requireActivity(),
            object : KeyboardVisibilityEventListener {
              override fun onVisibilityChanged(isOpen: Boolean) {
                animateFieldsRelatedToKeyboard(isOpen)
              }
            })
        requireActivity().lifecycle.removeObserver(this)
      }
    })

@yshrsmz
Copy link
Owner

yshrsmz commented Feb 12, 2021

possible fix for this issue is released in 3.0.0-RC3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants