Skip to content
This repository was archived by the owner on May 1, 2024. It is now read-only.

System.NotSupportedException: Unable to activate instance of type Xamarin.Forms.Platform.Android.SearchBarRenderer from native handle 0x7fe862fc54 (key_handle 0xb1c5ad6). #6550

Closed
Awinesh opened this issue Jun 17, 2019 · 12 comments
Labels
blocker Issue blocks next stable release. Prioritize fixing and reviewing this issue. e/3 🕒 3 i/high Completely doesn't work, crashes, or is unusably slow, has no obvious workaround; occurs less often p/Android t/bug 🐛

Comments

@Awinesh
Copy link

Awinesh commented Jun 17, 2019

There is Xamarin form using searchbar control at title view of navigation page. It throws exception on rotation of android device when control is active i.e control has focus and opened keyword to enter text.

Steps to Reproduce

  1. Create a project with xamarin form 4. Override titleview of navigation page form content page and use a searchbar in it.
  2. Run application in android.
  3. Select searchbar to enter text.
  4. Rotate device to landscape mode. It will throw this error.
    System.NotSupportedException: Unable to activate instance of type Xamarin.Forms.Platform.Android.SearchBarRenderer from native handle 0x7fe862fc54 (key_handle 0xb1c5ad6).
  • IDE: Visual studio 2017 professional
    Xamarin form 4.1.0.496342-pre2
    Android device: Samsung SM-M305F
    Android version: 8.1.0
@Awinesh Awinesh added s/unverified New report that has yet to be verified t/bug 🐛 labels Jun 17, 2019
@kvpt
Copy link
Contributor

kvpt commented Jun 17, 2019

This error is related to a problem in the dispose.
I'm trying to fix most of them but I haven't encounter this one yet.
Can you provide a sample which reproduce the issue please.

@rmarinho rmarinho added s/needs-repro ❔ This reported issue doesn't include a sample project reproducing the issue. Please provide one. p/Android blocker Issue blocks next stable release. Prioritize fixing and reviewing this issue. and removed s/needs-repro ❔ This reported issue doesn't include a sample project reproducing the issue. Please provide one. s/unverified New report that has yet to be verified labels Jun 17, 2019
@rmarinho rmarinho added this to the 4.0.0 milestone Jun 17, 2019
@rmarinho rmarinho added the e/3 🕒 3 label Jun 17, 2019
rmarinho added a commit that referenced this issue Jun 17, 2019
@rmarinho
Copy link
Member

I added a branch with reproduction, seems that when the toolbar is being reset, and when removing the nativeview, this normally is because it was disposed before. Doesn''t happen with a Entry for example.

@kvpt
Copy link
Contributor

kvpt commented Jun 17, 2019

@rmarinho Thanks for the repro.
Do you reproduce the issue yourself ?
I tested it but I don't reproduce it.
I focus the SearchBar and rotate the device (emulator), nothing happen.
I went back to the home page and retried several times with no success.

What is strange with this case is that dispose is normaly triggered after the page has been popped and not rotated, unless the activity is configured to not handle the orientation change.

@kvpt
Copy link
Contributor

kvpt commented Jun 17, 2019

I already made some changes in the NavigationPageRenderer dispose method.
But others methods need changes :

@samhouts samhouts added the i/high Completely doesn't work, crashes, or is unusably slow, has no obvious workaround; occurs less often label Jun 18, 2019
@kvpt
Copy link
Contributor

kvpt commented Jun 19, 2019

I succeed to reproduce it.

Here the complete stacktrace.

0x3B in Xamarin.Forms.Platform.Android.AppCompat.NavigationPageRenderer.ResetToolbar at D:\Workspace\Xamarin\Xamarin.Forms\Xamarin.Forms.Platform.Android\AppCompat\NavigationPageRenderer.cs:718,4
0x27 in Xamarin.Forms.Platform.Android.AppCompat.NavigationPageRenderer.DeviceInfoPropertyChanged at D:\Workspace\Xamarin\Xamarin.Forms\Xamarin.Forms.Platform.Android\AppCompat\NavigationPageRenderer.cs:542,5
0x33 in System.Runtime.CompilerServices.AsyncVoidMethodBuilder.Start<Xamarin.Forms.Platform.Android.AppCompat.NavigationPageRenderer.<DeviceInfoPropertyChanged>d__72>
0x38 in Xamarin.Forms.Platform.Android.AppCompat.NavigationPageRenderer.DeviceInfoPropertyChanged
0x13 in Xamarin.Forms.Internals.DeviceInfo.OnPropertyChanged at D:\Workspace\Xamarin\Xamarin.Forms\Xamarin.Forms.Core\DeviceInfo.cs:49,7
0x2A in Xamarin.Forms.Internals.DeviceInfo.set_CurrentOrientation at D:\Workspace\Xamarin\Xamarin.Forms\Xamarin.Forms.Core\DeviceInfo.cs:21,5
0x37 in Xamarin.Forms.Forms.AndroidDeviceInfo.CheckOrientationChanged at D:\Workspace\Xamarin\Xamarin.Forms\Xamarin.Forms.Platform.Android\Forms.cs:377,6
0x8 in Xamarin.Forms.Forms.AndroidDeviceInfo.ConfigurationChanged at D:\Workspace\Xamarin\Xamarin.Forms\Xamarin.Forms.Platform.Android\Forms.cs:386,5
0x1B in Xamarin.Forms.Platform.Android.FormsAppCompatActivity.OnConfigurationChanged at D:\Workspace\Xamarin\Xamarin.Forms\Xamarin.Forms.Platform.Android\AppCompat\FormsAppCompatActivity.cs:66,4

As expected, it is the ResetToolbar method which is trigerred by the rotation change.

@kvpt
Copy link
Contributor

kvpt commented Jun 19, 2019

I can confirm that the latest modifications included in #6467 fix the issue.
The root cause here, is that the renderer is disposed before the view is removed.

@samhouts Do you want a separate PR for this one too ?

I also have a question on the UpdateTitleView method ?

void UpdateTitleView()

...

if (_titleViewRenderer != null)
{
  var reflectableType = _titleViewRenderer as System.Reflection.IReflectableType;
  var rendererType = reflectableType != null ? reflectableType.GetTypeInfo().AsType() : _titleViewRenderer.GetType();
  if (titleView == null || Registrar.Registered.GetHandlerTypeForObject(titleView) != rendererType)
  {
    if (_titleView != null)
      _titleView.Child = null;
    Android.Platform.ClearRenderer(_titleViewRenderer.View);
    _titleViewRenderer.Dispose();
    _titleViewRenderer = null;
  }
}

...

I think that this bloc can cause issue too.
Because it dispose the old renderer before recreate it later, and never detach the view.
So it is possible for me that for an interval of time the titleview have no renderer and can cause crash.
I don't understand why it is done like that (with the registrar check), so I not modified it.

@kvpt
Copy link
Contributor

kvpt commented Aug 31, 2019

@Awinesh As #6467 is now included in 4.2 branch, can you test if the latest 4.2 version fix your issue ?

@jfversluis
Copy link
Member

@Awinesh I'm going to assume that @kvpt's PR fixed this. If not, feel free to reopen this.

Thank you!

@Srikanth540-L
Copy link

Srikanth540-L commented May 8, 2020

@samhouts we are facing random crash similar to above crash
Xamarin forms version: 4.5
Stack Trace:
System.NotSupportedException: Unable to activate instance of type Xamarin.Forms.Platform.Android.FormsEditText from native handle 0x75 (key_handle 0x9d1c1f0).
at Java.Interop.TypeManager.CreateInstance (System.IntPtr handle, Android.Runtime.JniHandleOwnership transfer, System.Type targetType) [0x00182] in :0
at Java.Lang.Object.GetObject (System.IntPtr handle, Android.Runtime.JniHandleOwnership transfer, System.Type type) [0x000c1] in :0
at Java.Lang.Object._GetObject[T] (System.IntPtr handle, Android.Runtime.JniHandleOwnership transfer) [0x00017] in :0
at Java.Lang.Object.GetObject[T] (System.IntPtr handle, Android.Runtime.JniHandleOwnership transfer) [0x00000] in :0
at Android.App.Activity.get_CurrentFocus () [0x0001f] in :0
at Xamarin.Forms.Platform.Android.FormsAppCompatActivity.OnResume () [0x0001e] in <039dc6f9f40c4a668399f93931013d38>:0
at MEC_Mobile.Droid.MainActivity.OnResume () [0x00000] in :0
at Android.App.Activity.n_OnResume (System.IntPtr jnienv, System.IntPtr native__this) [0x00009] in :0
at (wrapper dynamic-method) System.Object.12(intptr,intptr)
--- End of inner exception stack trace ---
System.MissingMethodException: No constructor found for Xamarin.Forms.Platform.Android.FormsEditText::.ctor(System.IntPtr, Android.Runtime.JniHandleOwnership)
at Java.Interop.TypeManager.CreateProxy (System.Type type, System.IntPtr handle, Android.Runtime.JniHandleOwnership transfer) [0x00055] in :0
at Java.Interop.TypeManager.CreateInstance (System.IntPtr handle, Android.Runtime.JniHandleOwnership transfer, System.Type targetType) [0x00116] in :0
--- End of inner exception stack trace ---
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 md51d7c7f7526562c6ae09f0b54043c9be9.MainActivity.n_onResume(Native Method)
at md51d7c7f7526562c6ae09f0b54043c9be9.MainActivity.onResume(MainActivity.java:76)
at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1453)
at android.app.Activity.performResume(Activity.java:8116)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:4526)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:4572)
at android.app.servertransaction.ResumeActivityItem.execute(ResumeActivityItem.java:52)
at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:181)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:102)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2239)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:227)
at android.app.ActivityThread.main(ActivityThread.java:7805)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1026)

@samhouts
Copy link
Member

samhouts commented Jun 3, 2020

@Srikanth540-L Do you have a reproduction project you can share? Thanks!

@EDMIStephen
Copy link

I am getting a very similar issue to @Srikanth540-L . Raised a bug #11098

@EmilAlipiev
Copy link
Contributor

@Srikanth540-L did you find any solution for that? i have exactly same exception?

# for free to subscribe to this conversation on GitHub. Already have an account? #.
Labels
blocker Issue blocks next stable release. Prioritize fixing and reviewing this issue. e/3 🕒 3 i/high Completely doesn't work, crashes, or is unusably slow, has no obvious workaround; occurs less often p/Android t/bug 🐛
Projects
None yet
Development

No branches or pull requests

8 participants