Bug 25280 - [Android] Setting the same page twice in Detail property in MasterDetailPage fails
Summary: [Android] Setting the same page twice in Detail property in MasterDetailPage ...
Status: RESOLVED INVALID
Alias: None
Product: Forms
Classification: Xamarin
Component: Forms ()
Version: 1.3.0
Hardware: Other Other
: --- normal
Target Milestone: ---
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2014-12-11 08:39 UTC by Tim Brand
Modified: 2014-12-12 04:29 UTC (History)
5 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED INVALID

Description Tim Brand 2014-12-11 08:39:18 UTC
When setting the Detail property in the MasterDetailPage for the same page (included in a NavigationPage) the 2nd time it creates an exception.

So I have a list of ContentPages. Everytime a menu-item is selected the Detail property will be changed by the selected page, the page will always be included in a new NavigationPage(), like this:

menuList.ItemSelected += (object sender, SelectedItemChangedEventArgs e) => {
    var page = e.SelectedItem as ContentPage;

    if (page != null) {
        Detail = new NavigationPage(page);
        IsPresented = false;
    }
};

When the same page is set on this way for the 2nd time, it creates an exception when setting the Detail property:
Java.Lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
  at at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () <IL 0x00011, 0x00068>
  at Android.Runtime.JNIEnv.CallNonvirtualVoidMethod (intptr,intptr,intptr,Android.Runtime.JValue[]) [0x00084] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.20-series/ba9bbbdd/source/monodroid/src/Mono.Android/src/Runtime/JNIEnv.g.cs:896
  at Android.Views.ViewGroup.AddView (Android.Views.View) [0x00070] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.20-series/ba9bbbdd/source/monodroid/src/Mono.Android/platforms/android-19/src/generated/Android.Views.ViewGroup.cs:1764
  at at Xamarin.Forms.Platform.Android.NavigationRenderer.SwitchContentAsync (Xamarin.Forms.Page,bool,bool) <IL 0x002d9, 0x0127f>
  at at Xamarin.Forms.Platform.Android.NavigationRenderer.OnPushAsync (Xamarin.Forms.Page,bool) <IL 0x00004, 0x00057>
  at at Xamarin.Forms.Platform.Android.NavigationRenderer.PushViewAsync (Xamarin.Forms.Page,bool) <IL 0x00003, 0x0005f>
  at at Xamarin.Forms.Platform.Android.NavigationRenderer.<OnElementChanged>b__0 (Xamarin.Forms.Page) <IL 0x00003, 0x0004f>
  at at Xamarin.Forms.EnumerableExtensions.ForEach<Xamarin.Forms.Page> (System.Collections.Generic.IEnumerable`1<Xamarin.Forms.Page>,System.Action`1<Xamarin.Forms.Page>) <0x00157>
  at at Xamarin.Forms.Platform.Android.NavigationRenderer.OnElementChanged (Xamarin.Forms.Platform.Android.ElementChangedEventArgs`1<Xamarin.Forms.NavigationPage>) <IL 0x000ee, 0x0083b>
  at at Xamarin.Forms.Platform.Android.VisualElementRenderer`1<Xamarin.Forms.NavigationPage>.SetElement (Xamarin.Forms.NavigationPage) <0x0060b>
  at at Xamarin.Forms.Platform.Android.VisualElementRenderer`1<Xamarin.Forms.NavigationPage>.Xamarin.Forms.Platform.Android.IVisualElementRenderer.SetElement (Xamarin.Forms.VisualElement) <0x0012b>
  at at Xamarin.Forms.Platform.Android.RendererFactory.GetRenderer (Xamarin.Forms.VisualElement) <IL 0x00022, 0x0013f>
  at at Xamarin.Forms.Platform.Android.MasterDetailContainer.set_ChildView (Xamarin.Forms.VisualElement) <IL 0x0003b, 0x00177>
  at at Xamarin.Forms.Platform.Android.MasterDetailRenderer.UpdateDetail () <IL 0x00011, 0x00083>
  at at Xamarin.Forms.Platform.Android.MasterDetailRenderer.HandlePropertyChanged (object,System.ComponentModel.PropertyChangedEventArgs) <IL 0x0002c, 0x0013f>
  at at (wrapper delegate-invoke) <Module>.invoke_void_object_PropertyChangedEventArgs (object,System.ComponentModel.PropertyChangedEventArgs) <IL 0x00044, 0x0009f>
  at at (wrapper delegate-invoke) <Module>.invoke_void_object_PropertyChangedEventArgs (object,System.ComponentModel.PropertyChangedEventArgs) <IL 0x00027, 0x00067>
  at at Xamarin.Forms.BindableObject.OnPropertyChanged (string) <IL 0x00012, 0x000c7>
  at at Xamarin.Forms.MasterDetailPage.set_Detail (Xamarin.Forms.Page) <IL 0x00068, 0x00247>
  at XamarinFormsTests.Views.MasterDetailNavigation.<MasterDetailNavigation>m__0 (object,Xamarin.Forms.SelectedItemChangedEventArgs) [0x0001b] in /Users/tim/Projects/XamarinFormsTests/XamarinFormsTests/Views/MasterDetailNavigation.cs:41
  at at (wrapper delegate-invoke) System.EventHandler`1<Xamarin.Forms.SelectedItemChangedEventArgs>.invoke_void_object_TEventArgs (object,Xamarin.Forms.SelectedItemChangedEventArgs) <0x00067>
  at at Xamarin.Forms.ListView.OnSelectedItemChanged (Xamarin.Forms.BindableObject,object,object) <IL 0x0001c, 0x00103>
  at at Xamarin.Forms.BindableObject.SetValueActual (Xamarin.Forms.BindableProperty,Xamarin.Forms.BindableObject/BindablePropertyContext,object,bool,Xamarin.Forms.BindableObject/SetValueFlags,bool) <IL 0x000f8, 0x00713>
  at at Xamarin.Forms.BindableObject.SetValueCore (Xamarin.Forms.BindableProperty,object,Xamarin.Forms.BindableObject/SetValueFlags,Xamarin.Forms.BindableObject/SetValuePrivateFlags) <IL 0x002cb, 0x00daf>
  at at Xamarin.Forms.BindableObject.SetValueCore (Xamarin.Forms.BindableProperty,object,Xamarin.Forms.BindableObject/SetValueFlags) <IL 0x00005, 0x00067>
  at at Xamarin.Forms.ListView.NotifyRowTapped (int,int) <IL 0x00051, 0x00287>
  at at Xamarin.Forms.ListView.NotifyRowTapped (int) <IL 0x00025, 0x0014b>
  at at Xamarin.Forms.Platform.Android.ListViewAdapter.OnItemClick (Android.Widget.AdapterView,Android.Views.View,int,long) <IL 0x00016, 0x000af>
  at Android.Widget.AdapterView/IOnItemClickListenerInvoker.n_OnItemClick_Landroid_widget_AdapterView_Landroid_view_View_IJ (intptr,intptr,intptr,intptr,int,long) [0x00019] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.20-series/ba9bbbdd/source/monodroid/src/Mono.Android/platforms/android-19/src/generated/Android.Widget.AdapterView.cs:194
  at at (wrapper dynamic-method) object.6d485802-a69a-4f98-8ebb-4cbb4aacc397 (intptr,intptr,intptr,intptr,int,long) <IL 0x00029, 0x0009b>
  at --- End of managed exception stack trace ---
  at java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
  at at android.view.ViewGroup.addViewInner(ViewGroup.java:3562)
  at at android.view.ViewGroup.addView(ViewGroup.java:3415)
  at at android.view.ViewGroup.addView(ViewGroup.java:3360)
  at at android.view.ViewGroup.addView(ViewGroup.java:3336)
  at at xamarin.forms.platform.android.ListViewAdapter.n_onItemClick(Native Method)
  at at xamarin.forms.platform.android.ListViewAdapter.onItemClick(ListViewAdapter.java:92)
  at at android.widget.AdapterView.performItemClick(AdapterView.java:298)
  at at android.widget.AbsListView.performItemClick(AbsListView.java:1113)
  at at android.widget.AbsListView$PerformClick.run(AbsListView.java:2911)
  at at android.widget.AbsListView$3.run(AbsListView.java:3645)
  at at android.os.Handler.handleCallback(Handler.java:733)
  at at android.os.Handler.dispatchMessage(Handler.java:95)
  at at android.os.Looper.loop(Looper.java:136)
  at at android.app.ActivityThread.main(ActivityThread.java:5146)
  at at java.lang.reflect.Method.invokeNative(Native Method)
  at at java.lang.reflect.Method.invoke(Method.java:515)
  at at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:796)
  at at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:612)
  at at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132)
  at at dalvik.system.NativeStart.main(Native Method)


Application log:
[Mono] DllImport attempting to load: '/system/lib/liblog.so'.
[Mono] DllImport loaded library '/system/lib/liblog.so'.
[Mono] DllImport searching in: '/system/lib/liblog.so' ('/system/lib/liblog.so').
[Mono] Searching for '__android_log_print'.
[Mono] Probing '__android_log_print'.
[Mono] Found as '__android_log_print'.
[MonoDroid] UNHANDLED EXCEPTION:
[MonoDroid] Java.Lang.IllegalStateException: Exception of type 'Java.Lang.IllegalStateException' was thrown.
[MonoDroid] at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () <IL 0x00011, 0x00068>
[MonoDroid] at Android.Runtime.JNIEnv.CallNonvirtualVoidMethod (intptr,intptr,intptr,Android.Runtime.JValue[]) [0x00084] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.20-series/ba9bbbdd/source/monodroid/src/Mono.Android/src/Runtime/JNIEnv.g.cs:896
[MonoDroid] at Android.Views.ViewGroup.AddView (Android.Views.View) [0x00070] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.20-series/ba9bbbdd/source/monodroid/src/Mono.Android/platforms/android-19/src/generated/Android.Views.ViewGroup.cs:1764
[MonoDroid] at Xamarin.Forms.Platform.Android.NavigationRenderer.SwitchContentAsync (Xamarin.Forms.Page,bool,bool) <IL 0x002d9, 0x0127f>
[MonoDroid] at Xamarin.Forms.Platform.Android.NavigationRenderer.OnPushAsync (Xamarin.Forms.Page,bool) <IL 0x00004, 0x00057>
[MonoDroid] at Xamarin.Forms.Platform.Android.NavigationRenderer.PushViewAsync (Xamarin.Forms.Page,bool) <IL 0x00003, 0x0005f>
[MonoDroid] at Xamarin.Forms.Platform.Android.NavigationRenderer.<OnElementChanged>b__0 (Xamarin.Forms.Page) <IL 0x00003, 0x0004f>
[MonoDroid] at Xamarin.Forms.EnumerableExtensions.ForEach<Xamarin.Forms.Page> (System.Collections.Generic.IEnumerable`1<Xamarin.Forms.Page>,System.Action`1<Xamarin.Forms.Page>) <0x00157>
[MonoDroid] at Xamarin.Forms.Platform.Android.NavigationRenderer.OnElementChanged (Xamarin.Forms.Platform.Android.ElementChangedEventArgs`1<Xamarin.Forms.NavigationPage>) <IL 0x000ee, 0x0083b>
[MonoDroid] at Xamarin.Forms.Platform.Android.VisualElementRenderer`1<Xamarin.Forms.NavigationPage>.SetElement (Xamarin.Forms.NavigationPage) <0x0060b>
[MonoDroid] at Xamarin.Forms.Platform.Android.VisualElementRenderer`1<Xamarin.Forms.NavigationPage>.Xamarin.Forms.Platform.Android.IVisualElementRenderer.SetElement (Xamarin.Forms.VisualElement) <0x0012b>
[MonoDroid] at Xamarin.Forms.Platform.Android.RendererFactory.GetRenderer (Xamarin.Forms.VisualElement) <IL 0x00022, 0x0013f>
[MonoDroid] at Xamarin.Forms.Platform.Android.MasterDetailContainer.set_ChildView (Xamarin.Forms.VisualElement) <IL 0x0003b, 0x00177>
[MonoDroid] at Xamarin.Forms.Platform.Android.MasterDetailRenderer.UpdateDetail () <IL 0x00011, 0x00083>
[MonoDroid] at Xamarin.Forms.Platform.Android.MasterDetailRenderer.HandlePropertyChanged (object,System.ComponentModel.PropertyChangedEventArgs) <IL 0x0002c, 0x0013f>
[MonoDroid] at (wrapper delegate-invoke) <Module>.invoke_void_object_PropertyChangedEventArgs (object,System.ComponentModel.PropertyChangedEventArgs) <IL 0x00044, 0x0009f>
[MonoDroid] at (wrapper delegate-invoke) <Module>.invoke_void_object_PropertyChangedEventArgs (object,System.ComponentModel.PropertyChangedEventArgs) <IL 0x00027, 0x00067>
[MonoDroid] at Xamarin.Forms.BindableObject.OnPropertyChanged (string) <IL 0x00012, 0x000c7>
[MonoDroid] at Xamarin.Forms.MasterDetailPage.set_Detail (Xamarin.Forms.Page) <IL 0x00068, 0x00247>
[MonoDroid] at XamarinFormsTests.Views.MasterDetailNavigation.<MasterDetailNavigation>m__0 (object,Xamarin.Forms.SelectedItemChangedEventArgs) [0x0001b] in /Users/tim/Projects/XamarinFormsTests/XamarinFormsTests/Views/MasterDetailNavigation.cs:41
[MonoDroid] at (wrapper delegate-invoke) System.EventHandler`1<Xamarin.Forms.SelectedItemChangedEventArgs>.invoke_void_object_TEventArgs (object,Xamarin.Forms.SelectedItemChangedEventArgs) <0x00067>
[MonoDroid] at Xamarin.Forms.ListView.OnSelectedItemChanged (Xamarin.Forms.BindableObject,object,object) <IL 0x0001c, 0x00103>
[MonoDroid] at Xamarin.Forms.BindableObject.SetValueActual (Xamarin.Forms.BindableProperty,Xamarin.Forms.BindableObject/BindablePropertyContext,object,bool,Xamarin.Forms.BindableObject/SetValueFlags,bool) <IL 0x000f8, 0x00713>
[MonoDroid] at Xamarin.Forms.BindableObject.SetValueCore (Xamarin.Forms.BindableProperty,object,Xamarin.Forms.BindableObject/SetValueFlags,Xamarin.Forms.BindableObject/SetValuePrivateFlags) <IL 0x002cb, 0x00daf>
[MonoDroid] at Xamarin.Forms.BindableObject.SetValueCore (Xamarin.Forms.BindableProperty,object,Xamarin.Forms.BindableObject/SetValueFlags) <IL 0x00005, 0x00067>
[MonoDroid] at Xamarin.Forms.ListView.NotifyRowTapped (int,int) <IL 0x00051, 0x00287>
[MonoDroid] at Xamarin.Forms.ListView.NotifyRowTapped (int) <IL 0x00025, 0x0014b>
[MonoDroid] at Xamarin.Forms.Platform.Android.ListViewAdapter.OnItemClick (Android.Widget.AdapterView,Android.Views.View,int,long) <IL 0x00016, 0x000af>
[MonoDroid] at Android.Widget.AdapterView/IOnItemClickListenerInvoker.n_OnItemClick_Landroid_widget_AdapterView_Landroid_view_View_IJ (intptr,intptr,intptr,intptr,int,long) [0x00019] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.20-series/ba9bbbdd/source/monodroid/src/Mono.Android/platforms/android-19/src/generated/Android.Widget.AdapterView.cs:194
[MonoDroid] at (wrapper dynamic-method) object.8217d86b-7397-4327-a448-d3cdbbd805da (intptr,intptr,intptr,intptr,int,long) <IL 0x00029, 0x0009b>
[MonoDroid]   --- End of managed exception stack trace ---
[MonoDroid] java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
[MonoDroid] 	at android.view.ViewGroup.addViewInner(ViewGroup.java:3562)
[MonoDroid] 	at android.view.ViewGroup.addView(ViewGroup.java:3415)
[MonoDroid] 	at android.view.ViewGroup.addView(ViewGroup.java:3360)
[MonoDroid] 	at android.view.ViewGroup.addView(ViewGroup.java:3336)
[MonoDroid] 	at xamarin.forms.platform.android.ListViewAdapter.n_onItemClick(Native Method)
[MonoDroid] 	at xamarin.forms.platform.android.ListViewAdapter.onItemClick(ListViewAdapter.java:92)
[MonoDroid] 	at android.widget.AdapterView.performItemClick(AdapterView.java:298)
[MonoDroid] 	at android.widget.AbsListView.performItemClick(AbsListView.java:1113)
[MonoDroid] 	at android.widget.AbsListView$PerformClick.run(AbsListView.java:2911)
[MonoDroid] 	at android.widget.AbsListView$3.run(AbsListView.java:3645)
[MonoDroid] 	at android.os.Handler.handleCallback(Handler.java:733)
[MonoDroid] 	at android.os.Handler.dispatchMessage(Handler.java:95)
[MonoDroid] 	at android.os.Looper.loop(Looper.java:136)
[MonoDroid] 	at android.app.ActivityThread.main(ActivityThread.java:5146)
[MonoDroid] 	at java.lang.reflect.Method.invokeNative(Native Method)
[MonoDroid] 	at java.lang.reflect.Method.invoke(Method.java:515)
[MonoDroid] 	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:796)
[MonoDroid] 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:612)
[MonoDroid] 	at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132)
[MonoDroid] 	at dalvik.system.NativeStart.main(Native Method)
[libc] Fatal signal 6 (SIGABRT) at 0x00007bb8 (code=-6), thread 31672 (msTests.Android)


My XF test sample app can be used to reproduce the error.
GitHub: https://github.com/App-Artment/xamarin-forms-tests

Steps to reproduce:
- Open app
- "tap here to start"
- Select MasterDetail Navigation
- Open menu and select page 1
- Open menu and select page 2
- Open menu and select page 1 <-- This will trigger the exception


Version info:
Xamarin Forms: 1.3.0.6280-pre2

Xamarin Studio
Version 5.5.4 (build 15)
Runtime:
	Mono 3.10.0 ((detached/92c4884)
	GTK+ 2.24.23 (Raleigh theme)

	Package version: 310000031

Xamarin.Android
Version: 4.20.0.28 (Indie Edition)
Android SDK: /Users/tim/Library/Developer/Xamarin/android-sdk-mac_x86
	Supported Android versions:
		2.1    (API level 7)
		2.2    (API level 8)
		2.3    (API level 10)
		3.1    (API level 12)
		3.2    (API level 13)
		4.0    (API level 14)
		4.0.3  (API level 15)
		4.1    (API level 16)
		4.2    (API level 17)
		4.3    (API level 18)
		4.4    (API level 19)
		4.4.87 (API level 20)
		5.0    (API level 21)
Java SDK: /usr/
java version "1.6.0_65"
Java(TM) SE Runtime Environment (build 1.6.0_65-b14-466.1-11M4716)
Java HotSpot(TM) 64-Bit Server VM (build 20.65-b04-466.1, mixed mode)
Comment 1 Rui Marinho 2014-12-11 13:57:52 UTC
The problem was with the code on the sample.

You shouldn't reuse a existing page on a new navigation page.

I made a pull request with a proper solution to your repo. 

https://github.com/App-Artment/xamarin-forms-tests/pull/1


Thanks
Rui
Comment 2 Tim Brand 2014-12-12 04:29:16 UTC
Thanks Rui, 
I've also did that before which resulted in another exception for my application. Will report that in another bug report as it's a different behavior on iOS and Android.