We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

InvalidCastException when adding a blank chart

Hello,
I am trying to use the SfChart control in an Android app, but I am getting a following exception when the page loads:

09-01 22:07:43.384 I/MonoDroid( 4191): UNHANDLED EXCEPTION:
09-01 22:07:43.443 I/MonoDroid( 4191): System.InvalidCastException: Specified cast is not valid.
09-01 22:07:43.443 I/MonoDroid( 4191):   at Com.Syncfusion.Charts.SfChart.BaseInitialize () [0x0005a] in <23c1f8fa601c46c78288431beb9f175a>:0 
09-01 22:07:43.443 I/MonoDroid( 4191):   at Com.Syncfusion.Charts.ChartBase.Initialize () [0x00000] in <23c1f8fa601c46c78288431beb9f175a>:0 
09-01 22:07:43.443 I/MonoDroid( 4191):   at Com.Syncfusion.Charts.ChartBase..ctor (Android.Content.Context context) [0x0001a] in <23c1f8fa601c46c78288431beb9f175a>:0 
09-01 22:07:43.443 I/MonoDroid( 4191):   at Com.Syncfusion.Charts.SfChart..ctor (Android.Content.Context context) [0x00000] in <23c1f8fa601c46c78288431beb9f175a>:0 
09-01 22:07:43.443 I/MonoDroid( 4191):   at Syncfusion.SfChart.XForms.Droid.SfChartExt..ctor (Android.Content.Context context) [0x00000] in <23c1f8fa601c46c78288431beb9f175a>:0 
09-01 22:07:43.443 I/MonoDroid( 4191):   at Syncfusion.SfChart.XForms.Droid.SfChartRenderer.GetNativeChart (Syncfusion.SfChart.XForms.SfChart sfChart) [0x00029] in <23c1f8fa601c46c78288431beb9f175a>:0 
09-01 22:07:43.443 I/MonoDroid( 4191):   at Syncfusion.SfChart.XForms.Droid.SfChartRenderer.OnElementChanged (Xamarin.Forms.Platform.Android.ElementChangedEventArgs`1[TElement] e) [0x00065] in <23c1f8fa601c46c78288431beb9f175a>:0 
09-01 22:07:43.443 I/MonoDroid( 4191):   at Xamarin.Forms.Platform.Android.VisualElementRenderer`1[TElement].SetElement (TElement element) [0x000f4] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Platform.Android\VisualElementRenderer.cs:196 
09-01 22:07:43.443 I/MonoDroid( 4191):   at Xamarin.Forms.Platform.Android.VisualElementRenderer`1[TElement].Xamarin.Forms.Platform.Android.IVisualElementRenderer.SetElement (Xamarin.Forms.VisualElement element) [0x00027] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Platform.Android\VisualElementRenderer.cs:137 
09-01 22:07:43.443 I/MonoDroid( 4191):   at Xamarin.Forms.Platform.Android.Platform.CreateRenderer (Xamarin.Forms.VisualElement element) [0x0001f] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Platform.Android\Platform.cs:289 
09-01 22:07:43.443 I/MonoDroid( 4191):   at Xamarin.Forms.Platform.Android.VisualElementPackager.AddChild (Xamarin.Forms.VisualElement view, Xamarin.Forms.Platform.Android.IVisualElementRenderer oldRenderer, Xamarin.Forms.Platform.Android.RendererPool pool, System.Boolean sameChildren) [0x00023] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Platform.Android\VisualElementPackager.cs:84 
09-01 22:07:43.443 I/MonoDroid( 4191):   at Xamarin.Forms.Platform.Android.VisualElementPackager.SetElement (Xamarin.Forms.VisualElement oldElement, Xamarin.Forms.VisualElement newElement) [0x00104] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Platform.Android\VisualElementPackager.cs:217 
09-01 22:07:43.443 I/MonoDroid( 4191):   at Xamarin.Forms.Platform.Android.VisualElementPackager.Load () [0x00000] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Platform.Android\VisualElementPackager.cs:68 
09-01 22:07:43.443 I/MonoDroid( 4191):   at Xamarin.Forms.Platform.Android.VisualElementRenderer`1[TElement].SetPackager (Xamarin.Forms.Platform.Android.VisualElementPackager packager) [0x00007] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Platform.Android\VisualElementRenderer.cs:335 
09-01 22:07:43.443 I/MonoDroid( 4191):   at Xamarin.Forms.Platform.Android.VisualElementRenderer`1[TElement].SetElement (TElement element) [0x00111] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Platform.Android\VisualElementRenderer.cs:199 
09-01 22:07:43.443 I/MonoDroid( 4191):   at Xamarin.Forms.Platform.Android.VisualElementRenderer`1[TElement].Xamarin.Forms.Platform.Android.IVisualElementRenderer.SetElement (Xamarin.Forms.VisualElement element) [0x00027] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Platform.Android\VisualElementRenderer.cs:137 
09-01 22:07:43.443 I/MonoDroid( 4191):   at Xamarin.Forms.Platform.Android.Platform.CreateRenderer (Xamarin.Forms.VisualElement element, Android.Support.V4.App.FragmentManager fragmentManager) [0x00031] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Platform.Android\Platform.cs:325 
09-01 22:07:43.443 I/MonoDroid( 4191):   at Xamarin.Forms.Platform.Android.AppCompat.FragmentContainer.OnCreateView (Android.Views.LayoutInflater inflater, Android.Views.ViewGroup container, Android.OS.Bundle savedInstanceState) [0x00008] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Platform.Android\AppCompat\FragmentContainer.cs:67 
09-01 22:07:43.443 I/MonoDroid( 4191):   at Android.Support.V4.App.Fragment.n_OnCreateView_Landroid_view_LayoutInflater_Landroid_view_ViewGroup_Landroid_os_Bundle_ (System.IntPtr jnienv, System.IntPtr native__this, System.IntPtr native_inflater, System.IntPtr native_container, System.IntPtr native_savedInstanceState) [0x00022] in <27c17fe440cf491ba8255bcefade6e02>:0 
09-01 22:07:43.443 I/MonoDroid( 4191):   at (wrapper dynamic-method) System.Object:18eebdef-a37d-416e-b57b-842ea12e62f8 (intptr,intptr,intptr,intptr,intptr)

The control is used in a very basic page, without any other controls, bindings etc, just as shown in the GettingStarted section of the documentation

I have the dependencies added as here
PCLSyncfusion.SfChart.XForms.dll
AndroidSyncfusion.SfChart.XForms.Android.dll
Syncfusion.SfChart.XForms.dll
Version 15.3.0.29
Xamarin forms version 2.3.4.267
Android API 23

Any idea why there is such an error?
Cheers



15 Replies

ME Manivannan Elangovan Syncfusion Team September 2, 2017 04:20 AM UTC

Hi Bartosz,

Sorry for the inconvenience caused. 
 
We have analyzed the reported issue with our sample, we are not able to reproduce the issue. Please clean and rebuild your application to remove old Syncfusion libraries.
  
Please make sure that the highlighted SfChart assemblies in attached screenshot are referred in your application.

Screenshot: http://www.syncfusion.com/downloads/support/forum/132442/ze/AssemblyReference545400369  
    
User guide link: https://help.syncfusion.com/xamarin/introduction/control-dependencies#sfchart

Please refer our release noteshttps://help.syncfusion.com/xamarin/release-notes/v15.3.0.26?type=all#breaking-changes

Please let us know if you have any queries. 

Thanks,
Manivannan E 



BJ Bartosz Jarmuz September 3, 2017 09:46 AM UTC

Thanks,

Seems that the issue was caused because I have added Syncfusion.Xamarin.SfChart.Android nuget to my android project... 

And the reason I added it is because when I only added Syncfusion.Xamarin.SfChart nuget across the solution, the control was not showing... (no error)

I have noticed that your controls require specifying the HeightReques property, otherwise they are not visible...

I've seen this in case of sfchart, sparkline and numericupdown (but it might be something wrong with all my views)




PS Parthiban Sundaram Syncfusion Team September 4, 2017 06:30 AM UTC

Hi Bartosz,

Thanks for the update.

We suspect that you were using the StackLayout to add our controls. For that, you need to set the HorizontalOptions and VerticalOptions of the control to set the height and width.

Reference link: https://developer.xamarin.com/guides/xamarin-forms/user-interface/layouts/stack-layout/#Sizing

If the above solution didn’t resolve the issue. Please update us sample or view code to reproduce the issue which will be helpful for providing further assistance on this.

Regards,
Parthiban S



BJ Bartosz Jarmuz September 4, 2017 07:01 AM UTC

Thanks!

That solves it indeed!



PS Parthiban Sundaram Syncfusion Team September 5, 2017 06:35 AM UTC

Hi Bartosz,

Thanks for the update.

We are glad to know that the given solution has been resolved at your end.

Regards,
Parthiban S



NA Naweed Akram January 19, 2018 02:51 AM UTC

Hi, 
I am having same problem. Cannot get to resolve it in Android. Followed all your instructions. Please see the attached solution for reference.

My project is .NET standard Xamarin.Form 2.5.0.

Thanks
Naweed

Attachment: App5_9e5f3414.rar


DV Divya Venkatesan Syncfusion Team January 19, 2018 04:14 PM UTC

Hi Naweed, 
  
The InvalidCastException exception is thrown because you have added Syncfusion.Xamarin.SfChart.Android  nuget package in Android project. We would like to let you know that it is not required to add Syncfusion.Xamarin.SfChart.Android and Syncfusion.Xamarin.SfChart.IOS nuget packages in Android and iOS projects. It is only for Xamarin.Android and Xamarin.iOS projects. Please add Syncfusion.Xamarin.SfChart alone in all the Xamarin.Forms projects. i.e PCL or NetStandard, Android, iOS and UWP. Please refer the following user guide to know more about the dependencies for Xamarin.Forms platform. 
  
  
Regards, 
Divya Venkatesan 
 



NA Naweed Akram January 20, 2018 05:02 PM UTC

Yes, removing the Android reference resolves the issue.

Thanks
Naweed


DV Divya Venkatesan Syncfusion Team January 22, 2018 09:53 AM UTC

Hi Naweed,

Thanks for the update.

We are glad to know that the given solution works. Please let us know if you need any further assistance.

Regards,
Divya Venkatesan


ER Erhan February 26, 2018 07:53 PM UTC

When I remove Syncfusion.Xamarin.SfChart.Android from the Android project, I get the following error instead:

Could not load type 'Com.Syncfusion.Charts.ChartRootLayout' from assembly 'Syncfusion.SfChart.Android, Version=16.1451.0.24, Culture=neutral, PublicKeyToken=null'.


DV Divya Venkatesan Syncfusion Team February 27, 2018 12:57 PM UTC

Hi Erhan, 
  
We suspect that SfChart assemblies are not referred properly in respective projects of your application.  
  
Please refer the following user guide to know more about the dependencies for Xamarin.Forms platform.  
  
Could you please check whether Syncfusion.SfChart.XForms.dll referenced in PCL project and Syncfusion.SfChart.XForms.Android.dll and Syncfusion.SfChart.XForms.dll are referenced in Android project of your application as in the below screenshot? 
  
 
  
  
Regards, 
Divya Venkatesan 
 



WS Waldemar Sudol March 15, 2018 09:59 AM UTC

I have same problem InvalidCastException with xamarin forms and .net standards 1.0 can you provide sample with working SfChart  ? If i add SfChart  + SfChart.Android to droid project i get InvalidCastException if only SfChart  i get Could not load type 'Com.Syncfusion.Charts.ChartRootLayout'


DV Divya Venkatesan Syncfusion Team March 15, 2018 03:34 PM UTC

Hi Waldemar,

We have prepared a sample based on your requirement with Xamarin.Forms and .NET Standards 1.0 which can be downloaded from the following link.

Sample: http://www.syncfusion.com/downloads/support/forum/132442/ze/TestChart1452594414

Please let us know if you need any further assistance.

Regards,
Divya Venkatesan



WS Waldemar Sudol March 16, 2018 07:28 AM UTC

The problem was xamarin.forms version. In your solution 2.5.0.121934 which works, most recent in my solution was a problem.


DV Divya Venkatesan Syncfusion Team March 19, 2018 10:28 AM UTC

Hi Waldemar,

We have prepared a sample with latest Xamarin.Forms version and it is working fine. We have attached the sample for your reference. Could you please check with the sample in below location?

Sample: http://www.syncfusion.com/downloads/support/forum/132442/ze/TestChart1659445743

Note: Please clean and rebuild your application.

If still you face the problem, please revert us by modifying the sample based on your application along with replication procedure. This would be helpful for us to serve you.

Regards,
Divya Venkatesan


Loader.
Live Chat Icon For mobile
Up arrow icon