How to handle navigation in a MasterDetailPage using Context Actions of a ListView ?

WildSalmonWildSalmon FRMember ✭✭

Hello there,

I will try to give an accurate explanation for the issue I'm facing.

Context :

I'm using a MasterDetailPage for my main navigation. In a Detail page (used as a NavigationPage), I display a ListView of items. Each item can be deleted or edited through Context Actions.
The Edit Action:
I want to navigate to another page (the page which allows editing of the selected item) as a PushAsync navigation. I also tried to set a new Detail element to the RootView to navigate to my page.
Both methods are set up in the App class.

PushAsync :

public static void PushNav(Page target) { RootView.Navigation.PushAsync(new NavigationPage(target)); }

Detail navigation :

public static void NavigateToDetail(Page target) { var rootPage = (RootView)rootView; rootPage.Detail = new NavigationPage(target); }

I tried both Command method and .Clicked event, they are both throwing errors.

var editWeezo = new MenuItem { Text = AppResources.ContextActionEdit, Command = new Command(() => { //App.NavigateToDetail(new DetailWeezoView()); App.PushNav(new DetailWeezoView()); }) }; editWeezo.SetBinding(MenuItem.CommandParameterProperty, new Binding(".")); //editWeezo.Clicked += EditWeezo_Clicked;

The issue:
When I'm trying to navigate from the page after completing the context action the app crashes and it throws an unhandled exception :

PushAsync method :

08-20 11:22:19.435 I/MonoDroid(23204): UNHANDLED EXCEPTION: 08-20 11:22:19.437 I/MonoDroid(23204): System.InvalidOperationException: PushAsync is not supported globally on Android, please use a NavigationPage. 08-20 11:22:19.437 I/MonoDroid(23204): at Xamarin.Forms.Platform.Android.Platform.PushAsync (Xamarin.Forms.Page,bool) <IL 0x0000a, 0x00064> 08-20 11:22:19.437 I/MonoDroid(23204): at Xamarin.Forms.NavigationProxy.OnPushAsync (Xamarin.Forms.Page,bool) <IL 0x00025, 0x00173> 08-20 11:22:19.437 I/MonoDroid(23204): at Xamarin.Forms.NavigationProxy.PushAsync (Xamarin.Forms.Page,bool) <IL 0x00016, 0x000d3> 08-20 11:22:19.437 I/MonoDroid(23204): at Xamarin.Forms.NavigationProxy.OnPushAsync (Xamarin.Forms.Page,bool) <IL 0x00025, 0x00173> 08-20 11:22:19.437 I/MonoDroid(23204): at Xamarin.Forms.NavigationProxy.PushAsync (Xamarin.Forms.Page,bool) <IL 0x00016, 0x000d3> 08-20 11:22:19.437 I/MonoDroid(23204): at Xamarin.Forms.NavigationProxy.PushAsync (Xamarin.Forms.Page) <IL 0x00003, 0x0004f> 08-20 11:22:19.437 I/MonoDroid(23204): at WeezoApplication.App.PushNav (Xamarin.Forms.Page) [0x00001] in D :\TFS\Weezo\WeezoApplication\WeezoApplication\WeezoApplication\App.cs:88 08-20 11:22:19.437 I/MonoDroid(23204): at WeezoApplication.View.ViewCell.ListWeezoViewCell/<>c.<.ctor>b__0_0 () [0x00001] in D :\TFS\Weezo\WeezoApplication\WeezoApplication\WeezoApplication\View\ViewCell\ListWeezoViewCell.cs:26 08-20 11:22:19.437 I/MonoDroid(23204): at Xamarin.Forms.Command/<>c__DisplayClass4_0.<.ctor>b__0 (object) <IL 0x00006, 0x00057> 08-20 11:22:19.437 I/MonoDroid(23204): at Xamarin.Forms.Command.Execute (object) <IL 0x00007, 0x0005b> 08-20 11:22:19.437 I/MonoDroid(23204): at Xamarin.Forms.MenuItem.Activate () <IL 0x0001c, 0x000ff> 08-20 11:22:19.437 I/MonoDroid(23204): at Xamarin.Forms.Platform.Android.CellAdapter.OnActionItemClicked (Android.Views.ActionMode,Android.Views.IMenuItem) <IL 0x00018, 0x000f3> 08-20 11:22:19.437 I/MonoDroid(23204): at Android.Views.ActionMode/ICallbackInvoker.n_OnActionItemClicked_Landroid_view_ActionMode_Landroid_view_MenuItem_ (intptr,intptr,intptr,intptr) [0x00019] in /Users/builder/data/lanes/1879/5f55a9ef/source/monodroid/src/Mono.Android/platforms/android-21/src/generated/Android.Views.ActionMode.cs:89 08-20 11:22:19.437 I/MonoDroid(23204): at (wrapper dynamic-method) object.9ebb5098-b40b-4897-96f5-75a6a4246824 (intptr,intptr,intptr,intptr) <IL 0x0001d, 0x00053> 08-20 11:22:19.443 W/art (23204): JNI RegisterNativeMethods: attempt to register 0 native methods for md52ce486a14f4bcd95899665e9d932190b.JavaProxyThrowable 08-20 11:22:19.445 D/AndroidRuntime(23204): Shutting down VM An unhandled exception occured.

NavigateToDetail() method :

08-20 11:10:18.262 I/MonoDroid(22511): UNHANDLED EXCEPTION: 08-20 11:10:18.265 I/MonoDroid(22511): System.ArgumentException: 'jobject' must not be IntPtr.Zero. 08-20 11:10:18.265 I/MonoDroid(22511): Parameter name: jobject 08-20 11:10:18.265 I/MonoDroid(22511): at Android.Runtime.JNIEnv.CallVoidMethod (intptr,intptr) [0x00010] in /Users/builder/data/lanes/1879/5f55a9ef/source/monodroid/src/Mono.Android/src/Runtime/JNIEnv.g.cs:546 08-20 11:10:18.265 I/MonoDroid(22511): at Android.Views.ActionModeInvoker.Finish () [0x0002d] in /Users/builder/data/lanes/1879/5f55a9ef/source/monodroid/src/Mono.Android/platforms/android-21/src/generated/Android.Views.ActionMode.cs:804 08-20 11:10:18.265 I/MonoDroid(22511): at Xamarin.Forms.Platform.Android.CellAdapter.OnActionItemClicked (Android.Views.ActionMode,Android.Views.IMenuItem) <IL 0x0001e, 0x00127> 08-20 11:10:18.265 I/MonoDroid(22511): at Android.Views.ActionMode/ICallbackInvoker.n_OnActionItemClicked_Landroid_view_ActionMode_Landroid_view_MenuItem_ (intptr,intptr,intptr,intptr) [0x00019] in /Users/builder/data/lanes/1879/5f55a9ef/source/monodroid/src/Mono.Android/platforms/android-21/src/generated/Android.Views.ActionMode.cs:89 08-20 11:10:18.265 I/MonoDroid(22511): at (wrapper dynamic-method) object.131b4de3-aed8-4d12-ac3c-68605ad2766e (intptr,intptr,intptr,intptr) <IL 0x0001d, 0x00053> 08-20 11:10:18.271 W/art (22511): JNI RegisterNativeMethods: attempt to register 0 native methods for md52ce486a14f4bcd95899665e9d932190b.JavaProxyThrowable 08-20 11:10:18.273 D/AndroidRuntime(22511): Shutting down VM An unhandled exception occured.

Cannot undertstand the "jobject" error.

Do you have any suggestion of how to navigate in this case of use ?

Tagged:

Best Answer

Answers

  • WildSalmonWildSalmon FRMember ✭✭

    @arroyos,
    Thanks but my issue is not the global MasterDetail page navigation. It is when I try to navigate (to a new page) after activating a Context Action in a ListView. Both cases PushAsync and MasterDetail navigation crash.

    I also tried this (that is something I use in other cases and it works fine - but not for a Context Action event/command) :

    var editWeezo = new MenuItem { Text = AppResources.ContextActionEdit, Command = new Command(() => { var rootPage = (RootView)App.Current.MainPage; rootPage.Detail = new NavigationPage(new DetailWeezoView()) { BarBackgroundColor = Color.FromHex(ColorResources.MainColor) }; //App.NavigateToDetail(new DetailWeezoView()); //App.PushNav(new DetailWeezoView()); }) };

    It crashes with :

    08-20 12:48:55.903 I/MonoDroid(18530): UNHANDLED EXCEPTION: 08-20 12:48:55.905 I/MonoDroid(18530): System.ArgumentException: 'jobject' must not be IntPtr.Zero. 08-20 12:48:55.905 I/MonoDroid(18530): Parameter name: jobject 08-20 12:48:55.905 I/MonoDroid(18530): at Android.Runtime.JNIEnv.CallVoidMethod (intptr,intptr) [0x00010] in /Users/builder/data/lanes/1879/5f55a9ef/source/monodroid/src/Mono.Android/src/Runtime/JNIEnv.g.cs:546 08-20 12:48:55.905 I/MonoDroid(18530): at Android.Views.ActionModeInvoker.Finish () [0x0002d] in /Users/builder/data/lanes/1879/5f55a9ef/source/monodroid/src/Mono.Android/platforms/android-21/src/generated/Android.Views.ActionMode.cs:804 08-20 12:48:55.905 I/MonoDroid(18530): at Xamarin.Forms.Platform.Android.CellAdapter.OnActionItemClicked (Android.Views.ActionMode,Android.Views.IMenuItem) <IL 0x0001e, 0x00127> 08-20 12:48:55.905 I/MonoDroid(18530): at Android.Views.ActionMode/ICallbackInvoker.n_OnActionItemClicked_Landroid_view_ActionMode_Landroid_view_MenuItem_ (intptr,intptr,intptr,intptr) [0x00019] in /Users/builder/data/lanes/1879/5f55a9ef/source/monodroid/src/Mono.Android/platforms/android-21/src/generated/Android.Views.ActionMode.cs:89 08-20 12:48:55.905 I/MonoDroid(18530): at (wrapper dynamic-method) object.d295267e-bb9a-4eca-a4d5-e9ae9020b0fa (intptr,intptr,intptr,intptr) <IL 0x0001d, 0x00053> 08-20 12:48:55.916 W/art (18530): JNI RegisterNativeMethods: attempt to register 0 native methods for md52ce486a14f4bcd95899665e9d932190b.JavaProxyThrowable 08-20 12:48:55.919 D/AndroidRuntime(18530): Shutting down VM An unhandled exception occured.

  • WildSalmonWildSalmon FRMember ✭✭

    No problem.

    Yes I have already been through this. But I couldn't solve the error.
    I also didn't see any other subjects concerning Context Action navigation issues.

    That's why I'm here tho ! :wink:

  • BrunoLMBrunoLM USMember

    Same issue. Xamarin.Forms 1.5.0.6404-pre3 AND 1.4.4

  • DannyNLDannyNL USUniversity ✭✭

    Same here. Current version Xamarin.Forms 1.5.0.6446.

    Occurs when using a context action on a listview in Android. No issue on iOS. When switched with the viewmodel command which is triggered on selection, than that one is causing the problem. So definitely related to the context action...

  • I'm also having issues with this. I have a MenuItem in a ListView linked up so a user can edit that row. When they hold on the row in Android and click Edit on the context bar my code tries to PushAsync and this happens. I've tried routing it through MessagingCenter but no luck.

    Not great.

  • NicoVermeirNicoVermeir BEUniversity ✭✭

    I managed to work around this by adding
    await Task.Delay(300);

    right before calling Navigate in the Clicked event handler of the MenuItem.
    Far from perfect but it stops this being a blocking issue for us.

  • TorbenKruseTorbenKruse DEMember ✭✭✭

    Task.Yield() should be the workaround here.

Sign In or Register to comment.