System.InvalidOperationException when trying to navigate back to Xamarim AppShell page with ListView containing ItemTemplate.

We have developed a Xamarin.Forms app that utilizes the SfListView with Swipe enabled and SfExpander inside the ListView ItemTemplate.  The app uses the Xamarin.Forms AppShell template.  When loading a page in the AppShell and then navigating away from it and coming back the error below occurs.  I have tried the to remove the child using FindByName(), but it comes back null.



System.InvalidOperationException: '-1,Element:crc64fa062d456cb98071.ExpanderHeader{aa8e6d7 V.E...... ......ID 0,0-0,0},Child:crc643f46942d9dd1fff9.Platform_DefaultRenderer{2fe3a5 V.E...... ......ID 0,0-1080,105}:Java.Lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.

  at Java.Interop.JniEnvironment+InstanceMethods.CallNonvirtualVoidMethod (Java.Interop.JniObjectReference instance, Java.Interop.JniObjectReference type, Java.Interop.JniMethodInfo method, Java.Interop.JniArgumentValue* args) [0x0008e] in <1959115d56f8444789986cf39185638c>:0

  at Java.Interop.JniPeerMembers+JniInstanceMethods.InvokeVirtualVoidMethod (System.String encodedMember, Java.Interop.IJavaPeerable self, Java.Interop.JniArgumentValue* parameters) [0x00063] in <1959115d56f8444789986cf39185638c>:0

  at Android.Views.ViewGroup.AddView (Android.Views.View child, System.Int32 index) [0x0003a] in /Users/builder/azdo/_work/2/s/xamarin-android/src/Mono.Android/obj/Release/monoandroid10/android-29/mcw/Android.Views.ViewGroup.cs:2062

  at Syncfusion.Platform.Mapping.XamarinExtensions.AddChildView (Android.Views.ViewGroup element, Android.Views.View childView, System.Int32 index) [0x00012] in <ee59555bdfa14e9fbc0ab2f09dddc8a3>:0

  --- End of managed Java.Lang.IllegalStateException stack trace ---

java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.

at android.view.ViewGroup.addViewInner(ViewGroup.java:5106)

at android.view.ViewGroup.addView(ViewGroup.java:4935)

at android.view.ViewGroup.addView(ViewGroup.java:4875)

at crc64fa062d456cb98071.SfExpander.n_onAttachedToWindow(Native Method)

at crc64fa062d456cb98071.SfExpander.onAttachedToWindow(SfExpander.java:56)

at android.view.View.dispatchAttachedToWindow(View.java:19553)

at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3430)

at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3437)

at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3437)

at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3437)

at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3437)

at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3437)

at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3437)

at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3437)

at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3437)

at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3437)

at android.view.ViewGroup.addViewInner(ViewGroup.java:5149)

at android.view.ViewGroup.addViewInLayout(ViewGroup.java:5082)

at android.view.ViewGroup.addViewInLayout(ViewGroup.java:5060)

at androidx.viewpager.widget.ViewPager.addView(ViewPager.java:1483)

at android.view.ViewGroup.addView(ViewGroup.java:4875)

at android.view.ViewGroup.addView(ViewGroup.java:4848)

at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:326)

at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1199)

at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1368)

at androidx.fragment.app.FragmentManager.moveFragmentToExpectedState(FragmentManager.java:1446)

at androidx.fragment.app.BackStackRecord.executeOps(BackStackRecord.java:442)

at androidx.fragment.app.FragmentManager.executeOps(FragmentManager.java:2181)

at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:2004)

at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:1959)

at androidx.fragment.app.FragmentManager.execSingleAction(FragmentManager.java:1830)

at androidx.fragment.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:303)

at androidx.fragment.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:246)

at androidx.viewpager.widget.ViewPager.populate(ViewPager.java:1244)

at androidx.viewpager.widget.ViewPager.populate(ViewPager.java:1092)

at androidx.viewpager.widget.ViewPager.onMeasure(ViewPager.java:1622)

at android.view.View.measure(View.java:24530)

at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6828)

at androidx.coordinatorlayout.widget.CoordinatorLayout.onMeasureChild(CoordinatorLayout.java:760)

at com.google.android.material.appbar.HeaderScrollingViewBehavior.onMeasureChild(HeaderScrollingViewBehavior.java:99)

at com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior.onMeasureChild(AppBarLayout.java:1996)

at androidx.coordinatorlayout.widget.CoordinatorLayout.onMeasure(CoordinatorLayout.java:831)

at android.view.View.measure(View.java:24530)

at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6828)

at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)

at android.view.View.measure(View.java:24530)

at android.widget.LinearLayout.measureVertical(LinearLayout.java:995)

at android.widget.LinearLayout.onMeasure(LinearLayout.java:721)

at android.view.View.measure(View.java:24530)

at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6828)

at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)

at android.view.View.measure(View.java:24530)

at androidx.drawerlayout.widget.DrawerLayout.onMeasure(DrawerLayout.java:1156)

at android.view.View.measure(View.java:24530)

at crc643f46942d9dd1fff9.PlatformRenderer.n_onMeasure(Native Method)

at crc643f46942d9dd1fff9.PlatformRenderer.onMeasure(PlatformRenderer.java:71)

at android.view.View.measure(View.java:24530)

at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:735)

at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:481)

at android.view.View.measure(View.java:24530)

at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6828)

at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)

at androidx.appcompat.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:146)

at android.view.View.measure(View.java:24530)

at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6828)

at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)

at android.view.View.measure(View.java:24530)

at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6828)

at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)

at android.view.View.measure(View.java:24530)

at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6828)

at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1552)

at android.widget.LinearLayout.measureVertical(LinearLayout.java:842)

at android.widget.LinearLayout.onMeasure(LinearLayout.java:721)

at android.view.View.measure(View.java:24530)

at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6828)

at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)

at com.android.internal.policy.DecorView.onMeasure(DecorView.java:742)

at android.view.View.measure(View.java:24530)

at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:3006)

at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1833)

at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2122)

at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1721)

at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7598)

at android.view.Choreographer$CallbackRecord.run(Choreographer.java:966)

at android.view.Choreographer.doCallbacks(Choreographer.java:790)

at android.view.Choreographer.doFrame(Choreographer.java:725)

at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:951)

at android.os.Handler.handleCallback(Handler.java:883)

at android.os.Handler.dispatchMessage(Handler.java:100)

at android.os.Looper.loop(Looper.java:214)

at android.app.ActivityThread.main(ActivityThread.java:7356)

at java.lang.reflect.Method.invoke(Native Method)

at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)

at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)


6 Replies 1 reply marked as answer

LN Lakshmi Natarajan Syncfusion Team July 8, 2021 03:42 AM UTC

Hi Chris, 
  
Thank you for using Syncfusion products. 
  
We have checked the reported query “System.InvalidOperationException when trying to navigate back to Xamarim AppShell page with ListView containing ItemTemplate” from our side. Unfortunately, we are unable to reproduce the reported scenario at our side. We have attached the tested sample and video in the following link, 
  
  
Additional information:  
Tested device:  Pixel 3XL Pie – 9.0 (Android 9.0 – API 28)  
SfListView version: 19.2.0.46  
Xamarin Forms: 5.0.0.2012  
Xamarin: 16.9.000.237  
Xamarin Designer: 16.9.0.316  
Xamarin Templates: 16.9.68  
Xamarin Android SDK: 11.2.2.1  
  
Could you please check our sample and let us know if you still facing the same issue? If not, please modify our sample to reproduce the issue and share the following details which will be helpful for us to check on it and provide you the solution as soon as possible. 
  • Share ListView and Expander code snippets
  • Share issue reproducing video
  • Share device configuration details
  • Share Syncfusion and Xamarin.Forms versions
  
Regards, 
Lakshmi Natarajan 


Marked as answer

CH Chris replied to Lakshmi Natarajan August 4, 2021 03:45 PM UTC

While your sample did remove the error, when I navigate back to the results page the ListView does not update with the ViewModel.  I have recreated the problem using the Xamarin.Forms standard ListView and it works fine.



LN Lakshmi Natarajan Syncfusion Team August 5, 2021 10:32 AM UTC

Hi Chris, 
 
Sorry for the inconvenience caused. 
 
Could you please modify the sample we shared in our previous update based on your scenario to reproduce the reported scenario with issue replication video? It will help us better understand your requirement and provide you with a solution as soon as possible. 
 
Lakshmi Natarajan 
 



CH Chris August 6, 2021 03:40 PM UTC

Due to some operations we do with Maps we don't use MVVM.  Is there another way to make this work that doesn't use MVVM?



CH Chris August 6, 2021 04:36 PM UTC

I finished recreating the issue using the ExpanderXamarin project and have attached it.  I created a new page called FilterPage that contains an Entry and a Button.  When I click the Filter button it goes through the ViewModel and removes all Contacts except for the name entered in the Entry.  When I then navigate back to the Browse page (ExpandableListView.xaml) by clicking on the tab bar button, the ListView has not updated to the filtered ViewModel.


Attachment: ExpanderXamarin_43ca9997.zip


LN Lakshmi Natarajan Syncfusion Team August 9, 2021 10:24 AM UTC

Hi Chris, 
 
Thank you for the update. 
 
We have checked the reported query “when I navigate back to the results page the ListView does not update with the ViewModel” from our side. We would like to inform you that you are creating separate ViewModel for ItemsPage and FilterPage. In this case, the BindingContext are not linked and the ItemsPage does know about the filter collection changes. Hence, we suggest you to use the ItemsPage’s BindingContext for the FilterPage to overcome the reported scenario. 
 
Please refer to the following code snippets to navigate to Filter page with the same BindingContext. 
<Grid x:Name="mainGrid" BackgroundColor="#F0F0F0" Padding="4"> 
    <Grid.RowDefinitions> 
        <RowDefinition Height="50"/> 
        <RowDefinition Height="*"/> 
    </Grid.RowDefinitions> 
 
    <Button Text="Filter" Command="{Binding GotoEditPageCommand}" /> 
         
        <sflistview:SfListView x:Name="listView" AutoFitMode="DynamicHeight" ItemsSource="{Binding ContactsInfo}" 
                            TapCommand="{Binding ItemTapped}" AllowSwiping="True" Grid.Row="1"> 
        ... 
    </sflistview:SfListView> 
</Grid> 
 
ViewModel: Set the BindingContext for the FilterPage. 
public class ViewModel : INotifyPropertyChanged 
{ 
    public ObservableCollection<Contact> ContactsInfo 
    { 
        get 
        {  
            return contactsInfo; 
        } 
        set 
        { 
            contactsInfo = value; 
            this.OnPropertyChanged("ContactsInfo"); 
        } 
    } 
 
    public Command<object> ItemTapped { get; } 
    public Command<string> FilterContacts { get; } 
    public Command<string> GotoEditPageCommand { get; } 
    #endregion 
 
    #region Constructor 
 
    public ViewModel() 
    { 
        ItemTapped = new Command<object>(OnItemSelected); 
        FilterContacts = new Command<string>(OnContactsFiltered); 
        GotoEditPageCommand = new Command<string>(OnGotoEditPage); 
        ... 
    } 
 
    private async void OnGotoEditPage(string obj) 
    { 
        var filterPage = new FilterPage(); 
        filterPage.BindingContext = this; 
        await App.Current.MainPage.Navigation.PushAsync(filterPage); 
    } 
... 
} 
 
Note: We need to invoke PropertyChanged event for the collection to update the changes in the UI. 
 
We have modified the sample to achieve your requirement and attached in the following link, 
 
 
  
Please let us know if you need further assistance. 
 
Lakshmi Natarajan 
 


Loader.
Up arrow icon