xabre / xamarin-forms-tab-badge

Xamarin Forms bindable Tab badges for iOS, Android, UWP, MacOS and WPF
MIT License
307 stars 75 forks source link

"System.MissingMethodException: No constructor found" when using a Singleton TabbedPage for Application.MainPage (Android) #67

Closed brminnick closed 5 years ago

brminnick commented 5 years ago

Description

(Only applicable on Android)

When Application.MainPage is a Singleton TabbedPage using a TabbedPageRenderer (example code, below), Xamarin.Forms.Platform.Android.AppCompat.Platform.SetPageInternal throws the following exceptions:

This is the same error from #4325 and continues to happen in v3.6.0264807.

Sample Code, TabbedPage

The complete sample reproduction code using Xamarin.Forms v3.6.0.344457 can be found here: https://github.com/brminnick/XamarinFormsSingletonRepro-Plugin.Badge

public class App : Application
{
    public App() => MainPage = SingletonTabbedPage.Instance;
}

public class SingletonTabbedPage : TabbedPage
{
    readonly static Lazy<SingletonTabbedPage> _instanceHolder = new Lazy<SingletonTabbedPage>(() => new SingletonTabbedPage());

    public static SingletonTabbedPage Instance => _instanceHolder.Value;
}

Sample Code, TabbedPageRenderer

using Xamarin.Forms;

using Android.Content;

using Xamarin.Forms.Platform.Android.AppCompat;

using XamarinFormsSingletonRepro;
using XamarinFormsSingletonRepro.Droid;

[assembly: ExportRenderer(typeof(SingletonTabbedPage), typeof(SingletonTabbedPageRenderer))]
namespace XamarinFormsSingletonRepro.Droid
{
    public class SingletonTabbedPageRenderer : TabbedPageRenderer
    {
        public SingletonTabbedPageRenderer(Context context) : base(context)
        {
        }
    }
}

Stack Trace

System.NotSupportedException: Unable to activate instance of type XamarinFormsSingletonRepro.Droid.SingletonTabbedPageRenderer from native handle 0x69 (key_handle 0x8858560). ---> System.MissingMethodException: No constructor found for XamarinFormsSingletonRepro.Droid.SingletonTabbedPageRenderer::.ctor(System.IntPtr, Android.Runtime.JniHandleOwnership) ---> Java.Interop.JavaLocationException: Exception of type 'Java.Interop.JavaLocationException' was thrown. at Java.Lang.Error: Exception of type 'Java.Lang.Error' was thrown. at java.lang.Error: Java callstack: at at mono.java.lang.RunnableImplementor.n_run(Native Method) at at mono.java.lang.RunnableImplementor.run(RunnableImplementor.java:30) at at android.os.Handler.handleCallback(Handler.java:873) at at android.os.Handler.dispatchMessage(Handler.java:99) at at android.os.Looper.loop(Looper.java:193) at at android.app.ActivityThread.main(ActivityThread.java:6669) at at java.lang.reflect.Method.invoke(Native Method) at at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) at at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) --- End of inner exception stack trace --- at Java.Interop.TypeManager.CreateProxy (System.Type type, System.IntPtr handle, Android.Runtime.JniHandleOwnership transfer) [0x00055] in <2960acf2eeb24d88b5230e1e8afbdc2e>:0 at Java.Interop.TypeManager.CreateInstance (System.IntPtr handle, Android.Runtime.JniHandleOwnership transfer, System.Type targetType) [0x00116] in <2960acf2eeb24d88b5230e1e8afbdc2e>:0 --- End of inner exception stack trace --- at Java.Interop.TypeManager.CreateInstance (System.IntPtr handle, Android.Runtime.JniHandleOwnership transfer, System.Type targetType) [0x00182] in <2960acf2eeb24d88b5230e1e8afbdc2e>:0 at Java.Lang.Object.GetObject (System.IntPtr handle, Android.Runtime.JniHandleOwnership transfer, System.Type type) [0x000c1] in <2960acf2eeb24d88b5230e1e8afbdc2e>:0 at Java.Lang.Object._GetObject[T] (System.IntPtr handle, Android.Runtime.JniHandleOwnership transfer) [0x00017] in <2960acf2eeb24d88b5230e1e8afbdc2e>:0 at Java.Lang.Object.GetObject[T] (System.IntPtr handle, Android.Runtime.JniHandleOwnership transfer) [0x00000] in <2960acf2eeb24d88b5230e1e8afbdc2e>:0 at Android.Views.ViewGroup.GetChildAt (System.Int32 index) [0x00036] in <2960acf2eeb24d88b5230e1e8afbdc2e>:0 at Xamarin.Forms.Platform.Android.AppCompat.Platform.SetPageInternal (Xamarin.Forms.Page newRoot) [0x0001a] in D:\a\1\s\Xamarin.Forms.Platform.Android\AppCompat\Platform.cs:301 at Xamarin.Forms.Platform.Android.AppCompat.Platform+<>cDisplayClass36_0.b__0 () [0x0001f] in D:\a\1\s\Xamarin.Forms.Platform.Android\AppCompat\Platform.cs:269 at Java.Lang.Thread+RunnableImplementor.Run () [0x00008] in <2960acf2eeb24d88b5230e1e8afbdc2e>:0 at Java.Lang.IRunnableInvoker.n_Run (System.IntPtr jnienv, System.IntPtr nativethis) [0x00009] in <2960acf2eeb24d88b5230e1e8afbdc2e>:0 at at (wrapper dynamic-method) Android.Runtime.DynamicMethodNameCounter.27(intptr,intptr)

Steps to Reproduce

  1. Open XamarinFormsSingletonRepro.sln
  2. Build/deploy XamarinFormsSingletonRepro.Android to an Android device
    • I am using a Pixel emulator running API 28
  3. Wait for the app to finish launching
  4. On the Android device, tap the hardware Back button to background the app
  5. On the Android device, tap the hardware Overview button to see backgrounded apps
  6. On the Android device, tap XamarinFormsSingletonRepro.Android to resume the app
  7. App crashes

Repro Recording

Basic Information

Reproduction Link

https://github.com/brminnick/XamarinFormsSingletonRepro-Plugin.Badge

Affected Devices

(From App Center Crash)

AffectedDevices

Environment

=== Visual Studio Enterprise 2019 for Mac ===

Version 8.0.2 (build 23) Installation UUID: 4ac69d79-c034-474c-9629-20084c942d3c GTK+ 2.24.23 (Raleigh theme) Xamarin.Mac 5.6.0.2 (d16-0 / 040682909)

Package version: 518010003

=== Mono Framework MDK ===

Runtime: Mono 5.18.1.3 (2018-08/fdb26b0a445) (64-bit) Package version: 518010003

=== NuGet ===

Version: 4.8.2.5835

=== .NET Core ===

Runtime: /usr/local/share/dotnet/dotnet Runtime Versions: 3.0.0-preview3-27503-5 3.0.0-preview-27324-5 3.0.0-preview-27122-01 2.2.0 2.2.0-preview3-27014-02 2.1.9 2.1.8 2.1.6 2.1.2 2.1.1 2.0.6 2.0.5 1.1.10 1.0.13 SDK: /usr/local/share/dotnet/sdk/2.2.100/Sdks SDK Versions: 2.2.100 2.1.505 2.1.504 2.1.500 2.1.302 2.1.301 2.1.101 2.1.4 1.1.11 MSBuild SDKs: /Library/Frameworks/Mono.framework/Versions/5.18.1/lib/mono/msbuild/15.0/bin/Sdks

=== Xamarin.Profiler ===

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

=== Updater ===

Version: 11

=== Xamarin.Android ===

Version: 9.2.0.5 (Visual Studio Enterprise) Android SDK: /Users/bramin/Library/Android/sdk Supported Android versions: 6.0 (API level 23) 7.0 (API level 24) 7.1 (API level 25) 8.0 (API level 26) 8.1 (API level 27)

SDK Tools Version: 26.1.1 SDK Platform Tools Version: 28.0.2 SDK Build Tools Version: 28.0.3

Build Information: Mono: mono/mono/2018-08-rc@5ad371dab1b Java.Interop: xamarin/java.interop/d16-0@c987483 LibZipSharp: grendello/LibZipSharp/master@44de300 LibZip: nih-at/libzip/rel-1-5-1@b95cf3f MXE: xamarin/mxe/xamarin@b9cbb535 ProGuard: xamarin/proguard/master@905836d SQLite: xamarin/sqlite/3.26.0@325e91a Xamarin.Android Tools: xamarin/xamarin-android-tools/d16-0@0a7edd6

=== Microsoft Mobile OpenJDK ===

Java SDK: /Users/bramin/Library/Developer/Xamarin/jdk/microsoft_dist_openjdk_8.0.25 1.8.0-25 Android Designer EPL code available here: https://github.com/xamarin/AndroidDesigner.EPL

=== Android Device Manager ===

Version: 1.2.0.14 Hash: 86df26f Branch: remotes/origin/d16-0 Build date: 2019-04-05 16:58:56 UTC

=== Apple Developer Tools ===

Xcode 10.2 (14490.120) Build 10E125

=== Xamarin.Mac ===

Version: 5.8.0.0 (Visual Studio Enterprise) Hash: 0aa84521 Branch: d16-0 Build date: 2019-04-02 16:01:19-0400

=== Xamarin.iOS ===

Version: 12.8.0.0 (Visual Studio Enterprise) Hash: 0aa84521 Branch: d16-0 Build date: 2019-04-02 16:01:19-0400

=== Xamarin Designer ===

Version: 4.17.4.418 Hash: 3d086e814 Branch: remotes/origin/d16-0 Build date: 2019-04-01 09:20:10 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: 800020023 Git revision: c576bb008dfe54cdcf596237e09564ebd2da4343 Build date: 2019-04-06 10:06:46+00 Build branch: master Xamarin extensions: 2c996830618e7cb8e8be97984d643eb86f122e8d

=== Operating System ===

Mac OS X 10.14.4 Darwin 18.5.0 Darwin Kernel Version 18.5.0 Mon Mar 11 20:40:32 PDT 2019 root:xnu-4903.251.3~3/RELEASE_X86_64 x86_64

=== Enabled user installed extensions ===

NuGet Package Management Extensions 0.17

brminnick commented 5 years ago

Solution

To fix the issue, rebuild Plugin.Badge targeting Xamarin.Forms v4.0. (I also recommend updating the Xamarin.Android Target Framework to v9.0.)

This Xamarin.Forms Issue shows that it is a bug in Xamarin.Forms that has been fixed in v4.0.

Since Xamarin.Forms v4.0 is still in preview, I recommend publishing a prerelease NuGet package of Plugin.Badge that is built targeting v4.0.

Workaround

For users also experiencing this crash, until a new Plugin.Badge NuGet package is published targeting Xamarin.Forms v4.0, here is a workaround:

xabre commented 5 years ago

Give 2.2.0-pre.1 a try

brminnick commented 5 years ago

Thanks @xabre! 2.2.0-pre.1 fixes the crash 👍

xabre commented 5 years ago

v2.2.0 also contains this fix