dotnet / android

.NET for Android provides open-source bindings of the Android SDK for use with .NET managed languages such as C#
MIT License
1.94k stars 533 forks source link

Crash: Attempt to invoke virtual method 'android.os.Handler android.support.v4.app.FragmentHostCallback.getHandler()' on a null object reference #3710

Closed knocte closed 4 years ago

knocte commented 5 years ago

Steps to Reproduce

(Sorry, I don't have a repro project because this crash is non-deterministic, sometimes it doesn't crash; when it does, I get it reported in my Sentry account.) My project, though, is: https://github.com/knocte/geewallet

Actual Behavior

Crash. Full raw exception logged:

AggregateException: One or more errors occurred. (Attempt to invoke virtual method 'android.os.Handler android.support.v4.app.FragmentHostCallback.getHandler()' on a null object reference)
  Module "GWallet.Frontend.XF.FrontendHelpers+MaybeCrash@182", line 0, col 0, in Invoke
    Void Invoke()
  Module "Java.Lang.Thread+RunnableImplementor", line 8, col 0, in Run
    Void Run()
  Module "Java.Lang.IRunnableInvoker", line 9, col 0, in n_Run
    Void n_Run(IntPtr, IntPtr)
  Module "(unknown)", col 0, in (unknown)
RuntimeException: Attempt to invoke virtual method 'android.os.Handler android.support.v4.app.FragmentHostCallback.getHandler()' on a null object reference
  Module "System.Runtime.ExceptionServices.ExceptionDispatchInfo", line 12, col 0, in Throw
    Void Throw()
  Module "Java.Interop.JniEnvironment+InstanceMethods", line 105, col 0, in CallBooleanMethod
    Boolean CallBooleanMethod(Java.Interop.JniObjectReference, Java.Interop.JniMethodInfo, Java.Interop.JniArgumentValue*)
  Module "Java.Interop.JniPeerMembers+JniInstanceMethods", line 20, col 0, in InvokeAbstractBooleanMethod
    Boolean InvokeAbstractBooleanMethod(System.String, Java.Interop.IJavaPeerable, Java.Interop.JniArgumentValue*)
  Module "Android.Support.V4.App.FragmentManagerInvoker", line 10, col 0, in ExecutePendingTransactions
    Boolean ExecutePendingTransactions()
  Module "Xamarin.Forms.Platform.Android.AppCompat.NavigationPageRenderer", line 246, col 0, in SwitchContentAsync
    System.Threading.Tasks.Task`1[System.Boolean] SwitchContentAsync(Xamarin.Forms.Page, Boolean, Boolean, Boolean)
  Module "Xamarin.Forms.Platform.Android.AppCompat.NavigationPageRenderer", line 23, col 0, in OnPopViewAsync
    System.Threading.Tasks.Task`1[System.Boolean] OnPopViewAsync(Xamarin.Forms.Page, Boolean)
  Module "Xamarin.Forms.Platform.Android.AppCompat.NavigationPageRenderer", line 0, col 0, in PopViewAsync
    System.Threading.Tasks.Task`1[System.Boolean] PopViewAsync(Xamarin.Forms.Page, Boolean)
  Module "Xamarin.Forms.Platform.Android.AppCompat.NavigationPageRenderer", line 14, col 0, in OnPopped
    Void OnPopped(System.Object, Xamarin.Forms.Internals.NavigationRequestedEventArgs)
  Module "Xamarin.Forms.NavigationPage+<Xamarin-Forms-INavigationPageController-RemoveAsyncInner>d__65", line 77, col 0, in MoveNext
    Void MoveNext()
  Module "System.Runtime.ExceptionServices.ExceptionDispatchInfo", line 12, col 0, in Throw
    Void Throw()
  Module "System.Runtime.CompilerServices.TaskAwaiter", line 62, col 0, in ThrowForNonSuccess
    Void ThrowForNonSuccess(System.Threading.Tasks.Task)
  Module "System.Runtime.CompilerServices.TaskAwaiter", line 40, col 0, in HandleNonSuccessAndDebuggerNotification
    Void HandleNonSuccessAndDebuggerNotification(System.Threading.Tasks.Task)
  Module "System.Runtime.CompilerServices.TaskAwaiter", line 8, col 0, in ValidateEnd
    Void ValidateEnd(System.Threading.Tasks.Task)
  Module "System.Runtime.CompilerServices.TaskAwaiter`1[[Xamarin.Forms.Page, Xamarin.Forms.Core, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null]]", line 0, col 0, in GetResult
    Xamarin.Forms.Page GetResult()
  Module "Xamarin.Forms.NavigationPage", line 147, col 0, in PopAsyncInner
    Void MoveNext()
  Module "System.Runtime.ExceptionServices.ExceptionDispatchInfo", line 12, col 0, in Throw
    Void Throw()
  Module "System.Runtime.CompilerServices.TaskAwaiter", line 62, col 0, in ThrowForNonSuccess
    Void ThrowForNonSuccess(System.Threading.Tasks.Task)
  Module "System.Runtime.CompilerServices.TaskAwaiter", line 40, col 0, in HandleNonSuccessAndDebuggerNotification
    Void HandleNonSuccessAndDebuggerNotification(System.Threading.Tasks.Task)
  Module "System.Runtime.CompilerServices.TaskAwaiter", line 8, col 0, in ValidateEnd
    Void ValidateEnd(System.Threading.Tasks.Task)
  Module "System.Runtime.CompilerServices.TaskAwaiter`1[[Xamarin.Forms.Page, Xamarin.Forms.Core, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null]]", line 0, col 0, in GetResult
    Xamarin.Forms.Page GetResult()
  Module "Xamarin.Forms.NavigationPage", line 274, col 0, in PopAsync
    Void MoveNext()

Expected Behavior

Shouldn't crash.

Version Information

Visual Studio Community 2019 for Mac
Version 8.3 (build 1805)
Installation UUID: 4000bdf7-5bd6-4590-a1c2-f5b59bb0bc69
    GTK+ 2.24.23 (Raleigh theme)
    Xamarin.Mac 5.16.1.24 (d16-3 / 08809f5b)

    Package version: 604000198

Mono Framework MDK
Runtime:
    Mono 6.4.0.198 (2019-06/fe64a4765e6) (64-bit)
    Package version: 604000198

NuGet
Version: 5.3.0.6192

.NET Core SDK
SDK: /usr/local/share/dotnet/sdk/3.0.100/Sdks
SDK Versions:
    3.0.100
    2.1.701
    2.1.700
    2.1.505
MSBuild SDKs: /Library/Frameworks/Mono.framework/Versions/6.4.0/lib/mono/msbuild/Current/bin/Sdks

.NET Core Runtime
Runtime: /usr/local/share/dotnet/dotnet
Runtime Versions:
    3.0.0
    2.1.13
    2.1.12
    2.1.11
    2.1.9

Xamarin.Profiler
Version: 1.6.12.26
Location: /Applications/Xamarin Profiler.app/Contents/MacOS/Xamarin Profiler

Updater
Version: 11

Xamarin.Android
Version: 10.0.0.43 (Visual Studio Community)
Commit: xamarin-android/d16-3/8af1ca8
Android SDK: /Users/knocte/Library/Developer/Xamarin/android-sdk-macosx
    Supported Android versions:
        8.0 (API level 26)
        8.1 (API level 27)

SDK Tools Version: 26.1.1
SDK Platform Tools Version: 29.0.2
SDK Build Tools Version: 27.0.3

Build Information: 
Mono: mono/mono/2019-06@7af64d1ebe9
Java.Interop: xamarin/java.interop/d16-3@5836f58
LibZipSharp: grendello/LibZipSharp/d16-3@71f4a94
LibZip: nih-at/libzip/rel-1-5-1@b95cf3f
ProGuard: xamarin/proguard/master@905836d
SQLite: xamarin/sqlite/3.27.1@8212a2d
Xamarin.Android Tools: xamarin/xamarin-android-tools/d16-3@cb41333

Microsoft Mobile OpenJDK
Java SDK: /Users/knocte/Library/Developer/Xamarin/jdk/microsoft_dist_openjdk_1.8.0.25
1.8.0-25
Android Designer EPL code available here:
https://github.com/xamarin/AndroidDesigner.EPL

Android SDK Manager
Version: 1.4.0.65
Hash: c33b107
Branch: remotes/origin/d16-3
Build date: 2019-09-19 20:42:44 UTC

Android Device Manager
Version: 1.2.0.115
Hash: 724ea69
Branch: remotes/origin/d16-3
Build date: 2019-09-19 20:43:06 UTC

Apple Developer Tools
Xcode 11.0 (14936)
Build 11A420a

Xamarin.Mac
Version: 6.2.0.42 (Visual Studio Community)
Hash: 5e8a208b
Branch: d16-3
Build date: 2019-09-17 14:19:30-0400

Xamarin.iOS
Version: 13.2.0.42 (Visual Studio Community)
Hash: 5e8a208b
Branch: d16-3
Build date: 2019-09-17 14:19:30-0400

Xamarin Designer
Version: 16.3.0.230
Hash: bbe518670
Branch: remotes/origin/d16-3-xcode11
Build date: 2019-09-17 18:39:30 UTC

Xamarin Inspector
Version: 1.4.3
Hash: db27525
Branch: 1.4-release
Build date: Mon, 09 Jul 2018 21:20:18 GMT
Client compatibility: 1

Build Information
Release ID: 803001805
Git revision: df50511ccb08d4d84a53939f1d7fc0b40cbb0ee1
Build date: 2019-09-20 16:10:55+00
Build branch: release-8.3
Xamarin extensions: da8d448cd65f2cc87c5e6dc7e2c2ca7b96d20c72

Operating System
Mac OS X 10.14.6
Darwin 18.7.0 Darwin Kernel Version 18.7.0
    Tue Aug 20 16:57:14 PDT 2019
    root:xnu-4903.271.2~2/RELEASE_X86_64 x86_64

More version info from the Sentry report:

Name | Mono
Version | 6.4.0
raw_description | Mono 6.4.0 (2019-06/7af64d1ebe9)

Name | Unix
Version | undefined
Kernel Version | 3.10.49.519
raw_description | Unix 3.10.49.5190

BouncyCastle.Crypto | 1.8.2.0
Common.Logging.Core | 3.4.1.0
FSharp.Core | 4.5.0.0
FSharp.Data | 3.0.0.0
FastAndroidCamera | 2.0.0.0
FormsViewGroup | 2.0.0.0
GWallet.Backend.NetStandard | 0.4.2.134
GWallet.Frontend.XF | 0.4.2.134
GWallet.Frontend.XF.Android | 0.4.2.134
HtmlAgilityPack | 1.11.9.0
Java.Interop | 0.1.0.0
JsonRpcSharp.Client | 1.0.0.0
JsonRpcSharp.IpcClient | 1.0.0.0
JsonRpcSharp.RpcClient | 1.0.0.0
JsonRpcSharp.TcpClient | 1.0.0.0
JsonRpcSharp.WebSocketClient | 1.0.0.0
Microsoft.CSharp | 2.0.5.0
Microsoft.Extensions.Logging.Abstractions | 1.0.2.0
Mono.Android | 0.0.0.0
Mono.Security | 2.0.5.0
NBitcoin | 4.1.1.80
NBitcoin.Altcoins | 1.0.1.35
Nethereum.ABI | 3.0.0.0
Nethereum.Accounts | 3.0.0.0
Nethereum.Contracts | 3.0.0.0
Nethereum.Hex | 3.0.0.0
Nethereum.KeyStore | 3.0.0.0
Nethereum.RLP | 3.0.0.0
Nethereum.RPC | 3.0.0.0
Nethereum.Signer | 3.0.0.0
Nethereum.StandardTokenEIP20 | 3.0.0.0
Nethereum.Util | 3.0.0.0
Nethereum.Web3 | 3.0.0.0
Newtonsoft.Json | 11.0.0.0
Plugin.Clipboard | 2.2.1.0
Plugin.Clipboard.Abstractions | 2.2.1.0
Plugin.Connectivity | 4.0.0.190
Plugin.Connectivity.Abstractions | 4.0.0.190
SharpRaven | 2.4.0.0
SkiaSharp | 1.68.0.0
SkiaSharp.Extended.Svg | 1.60.0.0
System | 2.0.5.0
System.Configuration.ConfigurationManager | 4.0.0.0
System.Core | 2.0.5.0
System.Data | 2.0.5.0
System.IO.Pipelines | 4.0.0.1
System.Net.Http | 4.0.0.0
System.Numerics | 2.0.5.0
System.Runtime.CompilerServices.Unsafe | 4.0.4.0
System.Runtime.Serialization | 2.0.5.0
System.ServiceModel.Internals | 0.0.0.0
System.Threading.Tasks.Extensions | 4.2.0.0
System.Xml | 2.0.5.0
System.Xml.Linq | 2.0.5.0
Xamarin.Android.Arch.Core.Common | 1.0.0.0
Xamarin.Android.Arch.Lifecycle.Common | 1.0.0.0
Xamarin.Android.Arch.Lifecycle.Runtime | 1.0.0.0
Xamarin.Android.FSharp.ResourceProvider.Runtime | 1.0.0.28
Xamarin.Android.Support.Animated.Vector.Drawable | 1.0.0.0
Xamarin.Android.Support.Annotations | 1.0.0.0
Xamarin.Android.Support.Compat | 1.0.0.0
Xamarin.Android.Support.Core.UI | 1.0.0.0
Xamarin.Android.Support.Core.Utils | 1.0.0.0
Xamarin.Android.Support.CustomTabs | 1.0.0.0
Xamarin.Android.Support.Design | 1.0.0.0
Xamarin.Android.Support.Fragment | 1.0.0.0
Xamarin.Android.Support.Media.Compat | 1.0.0.0
Xamarin.Android.Support.Transition | 1.0.0.0
Xamarin.Android.Support.Vector.Drawable | 1.0.0.0
Xamarin.Android.Support.v4 | 1.0.0.0
Xamarin.Android.Support.v7.AppCompat | 1.0.0.0
Xamarin.Android.Support.v7.CardView | 1.0.0.0
Xamarin.Android.Support.v7.MediaRouter | 1.0.0.0
Xamarin.Android.Support.v7.Palette | 1.0.0.0
Xamarin.Android.Support.v7.RecyclerView | 1.0.0.0
Xamarin.Essentials | 1.0.0.0
Xamarin.Forms.Core | 2.0.0.0
Xamarin.Forms.Platform | 1.0.0.0
Xamarin.Forms.Platform.Android | 2.0.0.0
Xamarin.Forms.Xaml | 2.0.0.0
ZXing.Net.Mobile.Core | 1.0.6963.20171
ZXing.Net.Mobile.Forms | 1.0.6963.20195
ZXing.Net.Mobile.Forms.Android | 1.0.0.0
ZXingNetMobile | 1.0.6963.20179
Zlib.Portable | 1.11.0.0
mscorlib | 2.0.5.0
zxing.portable | 0.16.2.0

Log File

(Only reproduced it in an app installed from the Android AppStore.)

Cheesebaron commented 5 years ago

I think this might be a Xamarin.Forms issue rather than a Xamarin.Android issue.

knocte commented 5 years ago

Are you sure?

BTW I realised this error seems to pop up to non-xamarin devs too:

https://stackoverflow.com/questions/41650721/attempt-to-invoke-virtual-method-android-os-handler-android-support-v4-app-frag

https://stackoverflow.com/questions/37941263/attempt-to-invoke-virtual-method-android-os-handler-android-support-v4-app-frag/40150360

dellis1972 commented 5 years ago

Looking at the various stack overflow links this seems like a bug in the java support libraries. I have a feeling this is something that Xamarin.Forms might need to handle.

knocte commented 5 years ago

You mean that XF code should add a catch(RuntimeException) block?

grendello commented 5 years ago

@Redth based on @dellis1972's suggestion, could you investigate on your side? Thanks!

Redth commented 5 years ago

@grendello my understanding of this is it's either an Android Support library bug (in which case, updating to the latest 28.0.0.3 is worth a try), or it's a bug in how Xamarin.Forms is using that API. If updating to 28.0.0.3 doesn't resolve the issue, I think the Xamarin.Forms team needs to look at this issue and see if they can work around the crash in their usage. Maybe @PureWeen has some thoughts?

PureWeen commented 5 years ago

Also with the support library update make sure to update the Target Framework to at least 9.0. I pulled down your project and noticed that it's still 8.1 https://github.com/knocte/geewallet/blob/frontend/src/GWallet.Frontend.XF.Android/GWallet.Frontend.XF.Android.fsproj#L13

With how the Support Library packages are put together if you are targeting 8.1 but have support 28 installed you'll still just be getting the bits for support 27

Reading through the reports it seems a lot of people encounter this with ViewPagers but the scenario you are using doesn't seem to involve a ViewPager. It also seems like everyone just kind of fixes this by finding something to override :-/

From the stack it seems like the exception is happening here in your code yes?

                currentPage.Navigation.InsertPageBefore(newPage, currentPage)
                currentPage.Navigation.PopAsync()
                    |> DoubleCheckCompletion

A few thoughts I would have on this would be

knocte commented 5 years ago

Also with the support library update make sure to update the Target Framework to at least 9.0

Thanks, in fact with 8.1 we still got the crash again even after upgrading the Support libs, so I'm upgrading TargetFramework now to 9.0 and will release new version of the app. If it keeps happening after this, will report back.

is the page being popped properly disposing of everything?

That pattern you highlight only happens twice in my app, once with the WelcomePage and another one with the LoadingPage. Both of these pages are very lightweight, by a first glance it seems I don't need to dispose anything on them, as they have very basic widgets, no timers, etc. In what cases do I need to dispose things?

Could you change this to doing a PushAsync and then Remove(currentPage) ? I'm just curious if adjusting that behavior a little bit would cause a different outcome?

I seem to recall that I tried this before and was having problems with it. Anyway, if after the upgrade to 9.0 I still get this crash, I'll try changing this and report back.

If you side load the apk you use for the app store or deploy from VS with all the same settings you use to produce the apk for the appstore can you reproduce?

Well this crash is so rare (have had only a single crash since I tried upgraded the Support libraries; but granted, I have just less than 10 installations of my app because it's not officially launched yet*) that I'm very sure I would not be able to reliably reproduce it by testing this.

* It's launched in the appstore in production, but I mean it's not being actively publicised.

knocte commented 5 years ago

Thanks, in fact with 8.1 we still got the crash again even after upgrading the Support libs, so I'm upgrading TargetFramework now to 9.0 and will release new version of the app. If it keeps happening after this, will report back.

BTW, after upgrading the TargetFramework from 8.1 to 9.0, VisualStudioForMac changed all the support libraries lines of packages.config file, like this:

-<package id="Xamarin.Android.Arch.Core.Common" version="1.1.1.3" targetFramework="monoandroid81" />
+<package id="Xamarin.Android.Arch.Core.Common" version="1.1.1.3" targetFramework="monoandroid81" requireReinstallation="true" />

Which makes me wonder:

Cheesebaron commented 5 years ago

Isn't it possible to use PackageReference in a fsharp project? This way you wouldn't have to mess around with tfm in packages.config files.

knocte commented 5 years ago

Isn't it possible to use PackageReference in a fsharp project?

Last time I tried that, ironically the build worked on VS4Mac but not in VSforWindows. There's some github issue about this, don't remember where (but must not be fixed yet if I'm subscribed and didn't get any updates about it).

knocte commented 5 years ago

(PS: not sure if the issue is XamAndroid projects not supporting this yet, or F#.)

knocte commented 4 years ago

Shouldn't the targetFramework attribute be monoandroid90 instead of monoandroid81 here?

In fact, this is what requireReinstallation="true" is for, but VS4Mac doesn't honor it (filed a different bug about it): https://github.com/mono/monodevelop/issues/9390

So I did a retargetting in VS for Windows, and released new binaries of my app, and it seems that since I use these new versions of the support libraries in the new monoandroid90 framework, this bug hasn't happened yet, so maybe it's already fixed.

knocte commented 4 years ago

If it keeps happening after this, will report back.

It didn't happen again, I'm closing. Thanks everyone.