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

Getting an odd error with rotator datasource

Hello !| 

I am trying to use rotator in normal layout, but I am getting an odd error saying " NullReferenceException: Object reference not set to an instance of an object.
 " while I am setting the datasource ! 

here is a snippet of my code 

     List<Com.Syncfusion.Rotator.SfRotatorItem> images = new List<Com.Syncfusion.Rotator.SfRotatorItem>();
            List<int> imageID = new List<int>();
            imageID.Add(Resource.Drawable.icon);
            Context context = this;

            Com.Syncfusion.Rotator.SfRotatorItem item;
            ImageView image;
            for (int i = 0; i < imageID.Count; i++)
            {
                item = new Com.Syncfusion.Rotator.SfRotatorItem(context);
                image = new ImageView(context);
                image.SetImageResource(imageID[i]);
                item.Content = image;
                images.Add(item);
            }

            Com.Syncfusion.Rotator.SfRotator rotator = FindViewById<Com.Syncfusion.Rotator.SfRotator> (Resource.Id.imageRotator);



6 Replies

ME Mark Eric May 6, 2019 01:43 PM

I am using this method as reference : 
https://help.syncfusion.com/xamarin-android/sfrotator/how-to


MK Muneesh Kumar G Syncfusion Team May 7, 2019 02:24 AM

Hi Mark Eric, 
 
Thanks for using Syncfusion products, 
 
We have analyzed your query and we would like to inform you that NullReferenceException occurs due to the control instance passing to SetContentView method. 
 
If you initialize the control in axml page, then we don’t need to pass the view to SetContentView method. You can resolve this problem by passing the axml page resource layout id itself as per the below code snippet. 
 
Code snippet: 
 
C#: 
 
SetContentView(Resource.Layout.activity_main); 
 
SfRotator rotator = FindViewById<SfRotator>(Resource.Id.sfRotator1); 
 
List<SfRotatorItem> rotatorItems = new List<SfRotatorItem>(); 
 
List<int> images = new List<int>(); 
images.Add(Resource.Drawable.movie1); 
images.Add(Resource.Drawable.movie2); 
images.Add(Resource.Drawable.movie3); 
 
for (int i = 0; i < images.Count; i++) 
{ 
    SfRotatorItem sfRotatorItem = new SfRotatorItem(this); 
    ImageView imageView = new ImageView(this); 
    imageView.SetImageResource(images[i]); 
    sfRotatorItem.Content = imageView; 
    rotatorItems.Add(sfRotatorItem); 
} 
 
rotator.DataSource = rotatorItems; 
 
 
We have created sample for your requirement. Please download the sample from below location. 
 
Please let us know if you have any queries.  
 
Regards, 
Muneesh Kumar G  



ME Mark Eric May 8, 2019 03:56 PM

Thanks so much for your help, Would you let me know how I can make the image stretch to fix the whole rotator? !


ME Mark Eric May 8, 2019 06:45 PM

Thanks , 
I already solved using 
    imageView.SetScaleType(ImageView.ScaleType.FitXy);



MK Muneesh Kumar G Syncfusion Team May 8, 2019 09:26 PM

Hi Mark Eric,  
 
Glad that the issue has been resolved and please get back to us if you need any other assistance.  
 
Regards,  
Muneesh Kumar G   



ME Mark Eric May 13, 2019 06:54 PM

Thanks ! 

Loader.
Live Chat Icon For mobile
Up arrow icon