App is crashing and getting Java.Lang.IllegalArgumentException: Cannot setMaxLifecycle for Fragment not attached to FragmentManager

Satya 160 Reputation points
2023-08-25T11:19:47.1533333+00:00

App is crashing some times and i am not able to find the root cause. Can anyone tell me by seeing the below crash log details and code for customtabrenderer

BottomBarTabbedPage is a custom control inheriting tabbedpage

Added a CompatibilityRenderer in ConfigureMauiHandlers in MauiProgram.cs

handlers.AddCompatibilityRenderer(typeof(BottomBarTabbedPage), typeof(CustomTabbedPageRenderer));

Thanks in advance.

 public class CustomTabbedPageRenderer : TabbedPageRenderer, NavigationBarView.IOnItemSelectedListener
    {

        private int bottomBarHeight = 0;
        private string lastPageTitle = "";
        private long lastRecoredTime = SystemClock.ElapsedRealtime();

        public CustomTabbedPageRenderer(Context context) : base(context)
        {

        }

        bool NavigationBarView.IOnItemSelectedListener.OnNavigationItemSelected(IMenuItem item)
        {
            if (lastPageTitle == item.TitleFormatted.ToString() && SystemClock.ElapsedRealtime() - lastRecoredTime >= 1000)
            {
                Element.CurrentPage.Navigation.PopToRootAsync().ConfigureAwait(true);
            }
            lastPageTitle = item.TitleFormatted.ToString();
            lastRecoredTime = SystemClock.ElapsedRealtime();
            return base.OnNavigationItemSelected(item); ;
        }

        protected override void OnElementChanged(ElementChangedEventArgs<TabbedPage> e)
        {

            base.OnElementChanged(e);
            if (e.OldElement == null && e.NewElement != null)
            {
                BottomNavigationView bottomNavigationView = GetBottomBar();
                if (bottomNavigationView != null)
                {
                    bottomBarHeight = bottomNavigationView.Height;
                }
                AdjustTabColors();
            }
        }

        protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            base.OnElementPropertyChanged(sender, e);
            if (e.PropertyName == nameof(TabbedPage.CurrentPage))
            {
                AdjustTabColors();
            }
            else if (e.PropertyName == BottomBarTabbedPage.IsBottomBarVisibleProperty.PropertyName)
            {
                var bottomBar = GetBottomBar();
                if (bottomBar != null && sender is BottomBarTabbedPage)
                {
                    var parameters = bottomBar.LayoutParameters;
                    if (bottomBarHeight == 0)
                    {
                        bottomBarHeight = bottomBar.Height;
                    }
                    parameters.Height = (sender as BottomBarTabbedPage).IsBottomBarVisible ? bottomBarHeight : 0;
                    bottomBar.LayoutParameters = parameters;
                }
            }
        }

        private void AdjustTabColors()
        {
            var bottomBar = GetBottomBar();
            if (bottomBar != null)
            {
                int childCount = bottomBar.ChildCount;
                for (int k = 0; k < childCount; k++)
                {
                    Android.Views.View view = bottomBar.GetChildAt(k);
                    ViewGroup bottomMenuView = (ViewGroup)view;
                    int menuChild = bottomMenuView.ChildCount;
                    for (int l = 0; l < menuChild; l++)
                    {
                        Android.Views.View view1 = bottomMenuView.GetChildAt(l);
                        NavigationBarItemView itemView = (NavigationBarItemView)view1;

                        if (itemView.ItemData.IsChecked)
                        {
                            lastPageTitle = itemView.ItemData.TitleFormatted.ToString();
                            view1.SetBackgroundColor(Android.Graphics.Color.ParseColor(AppColors.White));
                        }
                        else
                        {
                            view1.SetBackgroundColor(Android.Graphics.Color.ParseColor(AppColors.White));
                        }
                    }
                }
            }
        }

        public override int ChildCount => base.ChildCount;

        private BottomNavigationView GetBottomBar()
        {
            for (int i = 0; i <= ChildCount - 1; i++)
            {
                var childView = this.GetChildAt(i);
                if (childView is ViewGroup viewGroup)
                {
                    for (int j = 0; j <= viewGroup.ChildCount - 1; j++)
                    {
                        var childRelativeLayoutView = viewGroup.GetChildAt(j);
                        if (childRelativeLayoutView is BottomNavigationView)
                        {
                            var bottomBar = (BottomNavigationView)childRelativeLayoutView;
                            return bottomBar;
                        }
                    }
                }
            }
            return null;
        }


    }

androidx.fragment.app.BackStackRecord.setMaxLifecycle

BackStackRecord.java, line 260

Java.Lang.IllegalArgumentException: Cannot setMaxLifecycle for Fragment not attached to FragmentManager FragmentManager{6c265b8 in null}}

Java.Lang.IllegalArgumentException: Cannot setMaxLifecycle for Fragment not attached to FragmentManager FragmentManager{f7964e2 in null}}
  at Java.Interop.JniEnvironment.InstanceMethods.CallNonvirtualVoidMethod(JniObjectReference , JniObjectReference , JniMethodInfo , JniArgumentValue* )
  at Java.Interop.JniPeerMembers.JniInstanceMethods.InvokeVirtualVoidMethod(String , IJavaPeerable , JniArgumentValue* )
  at AndroidX.ViewPager.Widget.ViewPager.SetCurrentItem(Int32 , Boolean )
  at Microsoft.Maui.Controls.Compatibility.Platform.Android.AppCompat.TabbedPageRenderer.ScrollToCurrentPage()
  at Microsoft.Maui.Controls.Compatibility.Platform.Android.AppCompat.TabbedPageRenderer.OnElementPropertyChanged(Object , PropertyChangedEventArgs )
  at TLM.NextGenMobileApp.Droid.Renderers.CustomTabbedPageRenderer.OnElementPropertyChanged(Object sender, PropertyChangedEventArgs e)
  at Microsoft.Maui.Controls.BindableObject.OnPropertyChanged(String propertyName)
  at Microsoft.Maui.Controls.Element.OnPropertyChanged(String propertyName)
  at Microsoft.Maui.Controls.MultiPage`1[[Microsoft.Maui.Controls.Page, Microsoft.Maui.Controls, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].OnPropertyChanged(String propertyName)
  at Microsoft.Maui.Controls.MultiPage`1[[Microsoft.Maui.Controls.Page, Microsoft.Maui.Controls, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].set_CurrentPage(Page value)
  at Microsoft.Maui.Controls.Compatibility.Platform.Android.AppCompat.TabbedPageRenderer.OnNavigationItemSelected(IMenuItem )
  at TLM.NextGenMobileApp.Droid.Renderers.CustomTabbedPageRenderer.Google.Android.Material.Navigation.NavigationBarView.IOnItemSelectedListener.OnNavigationItemSelected(IMenuItem item)
  at Google.Android.Material.Navigation.NavigationBarView.IOnItemSelectedListenerInvoker.n_OnNavigationItemSelected_Landroid_view_MenuItem_(IntPtr , IntPtr , IntPtr )
  at Android.Runtime.JNINativeWrapper.Wrap_JniMarshal_PPL_Z(_JniMarshal_PPL_Z , IntPtr , IntPtr , IntPtr )
  at java.lang.IllegalArgumentException: Cannot setMaxLifecycle for Fragment not attached to FragmentManager FragmentManager{f7964e2 in null}}
  at androidx.fragment.app.BackStackRecord.setMaxLifecycle(BackStackRecord.java:260)
  at androidx.fragment.app.FragmentPagerAdapter.setPrimaryItem(FragmentPagerAdapter.java:219)
  at androidx.viewpager.widget.ViewPager.populate(ViewPager.java:1234)
  at androidx.viewpager.widget.ViewPager.setCurrentItemInternal(ViewPager.java:669)
  at androidx.viewpager.widget.ViewPager.setCurrentItemInternal(ViewPager.java:631)
  at androidx.viewpager.widget.ViewPager.setCurrentItem(ViewPager.java:623)
  at crc6438b0a6d327492b0a.CustomTabbedPageRenderer.n_onNavigationItemSelected(Native Method)
  at crc6438b0a6d327492b0a.CustomTabbedPageRenderer.onNavigationItemSelected(CustomTabbedPageRenderer.java:58)
  at com.google.android.material.navigation.NavigationBarView$1.onMenuItemSelected(NavigationBarView.java:291)
  at androidx.appcompat.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:836)
  at androidx.appcompat.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:159)
  at androidx.appcompat.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:987)
  at com.google.android.material.navigation.NavigationBarMenuView$1.onClick(NavigationBarMenuView.java:139)
  at android.view.View.performClick(View.java:8160)
  at android.view.View.performClickInternal(View.java:8137)
  at android.view.View.access$3700(View.java:888)
  at android.view.View$PerformClick.run(View.java:30236)
  at android.os.Handler.handleCallback(Handler.java:938)
  at android.os.Handler.dispatchMessage(Handler.java:99)
  at android.os.Looper.loop(Looper.java:246)
  at android.app.ActivityThread.main(ActivityThread.java:8615)
  at java.lang.reflect.Method.invoke(Native Method)
  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602)
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)

Developer technologies .NET .NET MAUI
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.