maui icon indicating copy to clipboard operation
maui copied to clipboard

NullReferenceException in UpdateLeftBarButtonItem

Open dantovalphas opened this issue 1 year ago • 10 comments

Description

Fatal Exception: android.runtime.JavaProxyThrowable: System.NullReferenceException: Object reference not set to an instance of an object at Microsoft.Maui.Controls.Platform.Compatibility.ShellToolbarTracker.get_MauiContext() at Microsoft.Maui.Controls.Platform.Compatibility.ShellToolbarTracker.UpdateLeftBarButtonItem(Context context, Toolbar toolbar, DrawerLayout drawerLayout, Page page) at System.Threading.Tasks.Task.<>c.<ThrowAsync>b__128_0(Object ) at Android.App.SyncContext.<>c__DisplayClass2_0.<Post>b__0() at Java.Lang.Thread.RunnableImplementor.Run() at Java.Lang.IRunnableInvoker.n_Run(IntPtr , IntPtr ) at Android.Runtime.JNINativeWrapper.Wrap_JniMarshal_PP_V(_JniMarshal_PP_V , IntPtr , IntPtr ) at mono.java.lang.RunnableImplementor.n_run(RunnableImplementor.java) at mono.java.lang.RunnableImplementor.run(RunnableImplementor.java:31) at android.os.Handler.handleCallback(Handler.java:873) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:211) at android.app.ActivityThread.main(ActivityThread.java:6769) at java.lang.reflect.Method.invoke(Method.java) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

Steps to Reproduce

Bug reported by Firebase Crashlytics in Android image com.alphas.growingfamily_issue_50301eaa3143b6359a31e950bc7a3b35_crash_session_6449CF32029A00014999FAE3E68583FE_DNE_0_v2_stacktrace.txt

Link to public reproduction project repository

none

Version with bug

7.0 (current)

Last version that worked well

7.0 (current)

Affected platforms

Android

Affected platform versions

Android 9

Did you find any workaround?

No response

Relevant log output

Fatal Exception: android.runtime.JavaProxyThrowable: System.NullReferenceException: Object reference not set to an instance of an object
   at Microsoft.Maui.Controls.Platform.Compatibility.ShellToolbarTracker.get_MauiContext()
   at Microsoft.Maui.Controls.Platform.Compatibility.ShellToolbarTracker.UpdateLeftBarButtonItem(Context context, Toolbar toolbar, DrawerLayout drawerLayout, Page page)
   at System.Threading.Tasks.Task.<>c.<ThrowAsync>b__128_0(Object )
   at Android.App.SyncContext.<>c__DisplayClass2_0.<Post>b__0()
   at Java.Lang.Thread.RunnableImplementor.Run()
   at Java.Lang.IRunnableInvoker.n_Run(IntPtr , IntPtr )
   at Android.Runtime.JNINativeWrapper.Wrap_JniMarshal_PP_V(_JniMarshal_PP_V , IntPtr , IntPtr )
       at mono.java.lang.RunnableImplementor.n_run(RunnableImplementor.java)
       at mono.java.lang.RunnableImplementor.run(RunnableImplementor.java:31)
       at android.os.Handler.handleCallback(Handler.java:873)
       at android.os.Handler.dispatchMessage(Handler.java:99)
       at android.os.Looper.loop(Looper.java:211)
       at android.app.ActivityThread.main(ActivityThread.java:6769)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

dantovalphas avatar Apr 27 '23 14:04 dantovalphas

We've added this issue to our backlog, and we will work to address it as time and resources allow. If you have any additional information or questions about this issue, please leave a comment. For additional info about issue management, please read our Triage Process.

ghost avatar Apr 27 '23 15:04 ghost

Hello, we have the same issue in our Shell app, on different devices and different Android versions (no recognizable patterns). We are not able to reproduce the issue but we find it often in our logs on Google Play Console. Please, take a look at this, it's causing lots of crashes.

michele-guion avatar Jul 04 '23 14:07 michele-guion

Can confirm this issue in our Shell app. Also found it via Crashlytics report. Similar stacktrace.

Microsoft.Maui.Controls.Platform.Compatibility.ShellToolbarTracker.get_MauiContext Microsoft.Maui.Controls.Platform.Compatibility.ShellToolbarTracker+<UpdateLeftBarButtonItem>d__51.MoveNext System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw System.Threading.Tasks.Task+<>c.<ThrowAsync>b__128_0 Android.App.SyncContext+<>c__DisplayClass2_0.<Post>b__0 Java.Lang.Thread+RunnableImplementor.Run Java.Lang.IRunnableInvoker.n_Run Android.Runtime.JNINativeWrapper.Wrap_JniMarshal_PP_V mono.java.lang.RunnableImplementor.n_run (RunnableImplementor.java) mono.java.lang.RunnableImplementor.run (RunnableImplementor.java:31) android.os.Handler.handleCallback (Handler.java:938) android.os.Handler.dispatchMessage (Handler.java:99) android.os.Looper.loopOnce (Looper.java:226) android.os.Looper.loop (Looper.java:313) android.app.ActivityThread.main (ActivityThread.java:8663) java.lang.reflect.Method.invoke (Method.java) com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:567) com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1135)

@jsuarezruiz : Are there any efforts to solve this problem in the near future? @michele-guion : Did you find a workaround or a solution to get around this problem?

Thx Stefan

Stefan171086 avatar Nov 09 '23 08:11 Stefan171086

@PureWeen Any solution for this? Still not works on version 8. I have the same issue when open application through deep link.

DimaKB avatar Nov 29 '23 16:11 DimaKB

Can confirm this issue in our Shell app. Also found it via Crashlytics report. Similar stacktrace.

Microsoft.Maui.Controls.Platform.Compatibility.ShellToolbarTracker.get_MauiContext Microsoft.Maui.Controls.Platform.Compatibility.ShellToolbarTracker+<UpdateLeftBarButtonItem>d__51.MoveNext System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw System.Threading.Tasks.Task+<>c.<ThrowAsync>b__128_0 Android.App.SyncContext+<>c__DisplayClass2_0.<Post>b__0 Java.Lang.Thread+RunnableImplementor.Run Java.Lang.IRunnableInvoker.n_Run Android.Runtime.JNINativeWrapper.Wrap_JniMarshal_PP_V mono.java.lang.RunnableImplementor.n_run (RunnableImplementor.java) mono.java.lang.RunnableImplementor.run (RunnableImplementor.java:31) android.os.Handler.handleCallback (Handler.java:938) android.os.Handler.dispatchMessage (Handler.java:99) android.os.Looper.loopOnce (Looper.java:226) android.os.Looper.loop (Looper.java:313) android.app.ActivityThread.main (ActivityThread.java:8663) java.lang.reflect.Method.invoke (Method.java) com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:567) com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1135)

@jsuarezruiz : Are there any efforts to solve this problem in the near future? @michele-guion : Did you find a workaround or a solution to get around this problem?

Thx Stefan

@Stefan171086 no workarounds, still happening, can't understand when and why. We found the issue looking into logs, but it never happened in debug or during tests.

michele-guion avatar Nov 30 '23 15:11 michele-guion

I found the root cause of this exception in our shell app. We had a scenario where we navigate from page A to page B and because of some conditions in page B it triggered a navigation immediately back to page A. And after this immediate back navigation this exception was thrown. Now we solved our problem. May this information helps the MAUI team to fix the BUG in there code. BR Stefan

Stefan171086 avatar Nov 30 '23 19:11 Stefan171086

I'm facing the same issue. I've tracked it down to fast navigation. For example, when the application starts, if you navigate to the LoginPage or the AboutPage from App.xaml.cs, depending on the authentication status, the error occurs. My current workaround is to have a default AuthenticationPage defined in the AppShell as ShellContentView. In the AuthenticationPage, I wait for a second before performing the navigation. If it helps, I can try to build a repository that reproduces the error.

meierhoeferjannis avatar May 17 '24 07:05 meierhoeferjannis

I'm facing this one too in MAUI v8.0.40 SR5

ADB log

[ERROR] FATAL UNHANDLED EXCEPTION: System.NullReferenceException: Object reference not set to an instance of an object.
   at Microsoft.Maui.Controls.Platform.Compatibility.ShellToolbarTracker.get_MauiContext() in D:\a\_work\1\s\src\Controls\src\Core\Compatibility\Handlers\Shell\Android\ShellToolbarTracker.cs:line 71
   at Microsoft.Maui.Controls.Platform.Compatibility.ShellToolbarTracker.UpdateLeftBarButtonItem(Context context, Toolbar toolbar, DrawerLayout drawerLayout, Page page) in D:\a\_work\1\s\src\Controls\src\Core\Compatibility\Handlers\Shell\Android\ShellToolbarTracker.cs:line 413
   at System.Threading.Tasks.Task.<>c.<ThrowAsync>b__128_0(Object state)
   at Android.App.SyncContext.<>c__DisplayClass2_0.<Post>b__0() in /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Android.App/SyncContext.cs:line 36
   at Java.Lang.Thread.RunnableImplementor.Run() in /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Java.Lang/Thread.cs:line 36
   at Java.Lang.IRunnableInvoker.n_Run(IntPtr jnienv, IntPtr native__this) in /Users/runner/work/1/s/xamarin-android/src/Mono.Android/obj/Release/net8.0/android-34/mcw/Java.Lang.IRunnable.cs:line 84
   at Android.Runtime.JNINativeWrapper.Wrap_JniMarshal_PP_V(_JniMarshal_PP_V callback, IntPtr jnienv, IntPtr klazz) in /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Android.Runtime/JNINativeWrapper.g.cs:line 22

https://github.com/dotnet/maui/blob/ecfa829f6395a0f1fee28a4670831e4b70e4e0b0/src/Controls/src/Core/Compatibility/Handlers/Shell/Android/ShellToolbarTracker.cs#L71

I can see that UpdateLeftBarButtonItem is declared async void which possibly will run async to the caller. Perhaps this is the culprit.

https://github.com/dotnet/maui/blob/ecfa829f6395a0f1fee28a4670831e4b70e4e0b0/src/Controls/src/Core/Compatibility/Handlers/Shell/Android/ShellToolbarTracker.cs#L370

espenrl avatar May 28 '24 19:05 espenrl

Maybe also something interesting I discovered recently: When you add Shell.NavBarIsVisible="False" Shell.FlyoutBehavior="Disabled" Shell.TabBarIsVisible="False" Shell.FlyoutItemIsVisible="False" to a page and you navigate back from it ( no matter how long you wait on the page) the same exception gets thrown. Maybe that helps to reproduce the issue. If necessary, I can build a sample repo. Hope that this issue gets fixed soon as it is very annoying.

meierhoeferjannis avatar Jun 17 '24 09:06 meierhoeferjannis

@meierhoeferjannis I'm available to fix this, but I couldn't reproduce from within the MAUI solution.

Can you build a sample repo?

albyrock87 avatar Jun 28 '24 17:06 albyrock87

@albyrock87 Hey, yeah i will try to create a sample repo and provide until next week if thats okay :)

meierhoeferjannis avatar Jul 03 '24 08:07 meierhoeferjannis

@meierhoeferjannis I managed to repro and create a PR with the fix, so don't worry (well, unless you discover an additional use case not covered by my fix).

albyrock87 avatar Jul 03 '24 11:07 albyrock87