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.
Unfortunately, activation email could not send to your email. Please try again.

InvalidCastException when adding a blank chart

Thread ID:

Created:

Updated:

Platform:

Replies:

132442 Sep 1,2017 06:14 PM Mar 19,2018 06:28 AM Xamarin.Forms 15
loading
Tags: SfChart
Bartosz Jarmuz
Asked On September 1, 2017 06:14 PM

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



Manivannan Elangovan [Syncfusion]
Replied On September 2, 2017 12:20 AM

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 


Bartosz Jarmuz
Replied On September 3, 2017 05:46 AM

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)



Parthiban Sundaram [Syncfusion]
Replied On September 4, 2017 02:30 AM

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


Bartosz Jarmuz
Replied On September 4, 2017 03:01 AM

Thanks!

That solves it indeed!


Parthiban Sundaram [Syncfusion]
Replied On September 5, 2017 02:35 AM

Hi Bartosz,

Thanks for the update.

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

Regards,
Parthiban S


Naweed Akram
Replied On January 18, 2018 09:51 PM

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

Divya Venkatesan [Syncfusion]
Replied On January 19, 2018 11:14 AM

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 
 


Naweed Akram
Replied On January 20, 2018 12:02 PM

Yes, removing the Android reference resolves the issue.

Thanks
Naweed

Divya Venkatesan [Syncfusion]
Replied On January 22, 2018 04:53 AM

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

Erhan
Replied On February 26, 2018 02:53 PM

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'.

Divya Venkatesan [Syncfusion]
Replied On February 27, 2018 07:57 AM

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 
 


Waldemar Sudol
Replied On March 15, 2018 05:59 AM

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'

Divya Venkatesan [Syncfusion]
Replied On March 15, 2018 11:34 AM

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


Waldemar Sudol
Replied On March 16, 2018 03:28 AM

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

Divya Venkatesan [Syncfusion]
Replied On March 19, 2018 06:28 AM

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


CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.

;