Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 58056

ArgumentOutOfRange on FontAwesome Label inside a ListView

$
0
0

I've got a functioning FontAwesome Renderer in my app. I'm able to see fonts properly except for when they're contained within a ListView. When I load it on Android, I get this error.

07-14 13:52:01.276 I/MonoDroid(26699): UNHANDLED EXCEPTION: System.ArgumentOutOfRangeException: Argument is out of range.
07-14 13:52:01.276 I/MonoDroid(26699): Parameter name: index
07-14 13:52:01.276 I/MonoDroid(26699): at System.Collections.Generic.List

1<Xamarin.Forms.Element>.get_Item (int) <0x0007c>
07-14 13:52:01.276 I/MonoDroid(26699): at System.Collections.ObjectModel.Collection
1<Xamarin.Forms.Element>.get_Item (int) <0x00083>

07-14 13:52:01.276 I/MonoDroid(26699): at System.Collections.ObjectModel.ReadOnlyCollection
1<Xamarin.Forms.Element>.get_Item (int) <0x00083>
07-14 13:52:01.276 I/MonoDroid(26699): at Xamarin.Forms.Platform.Android.VisualElementPackager.SetModel (Xamarin.Forms.VisualElement,Xamarin.Forms.VisualElement) <IL 0x0010b, 0x00947>
07-14 13:52:01.276 I/MonoDroid(26699): at Xamarin.Forms.Platform.Android.VisualElementPackager.<.ctor>b__0 (object,Xamarin.Forms.Platform.Android.VisualElementChangedEventArgs) <IL 0x0000d, 0x000a3>
07-14 13:52:01.276 I/MonoDroid(26699): at Xamarin.Forms.Platform.Android.VisualElementRenderer
1<Xamarin.Forms.View>.OnElementChanged (Xamarin.Forms.Platform.Android.ElementChangedEventArgs
1<Xamarin.Forms.View>) <0x00137>
07-14 13:52:01.276 I/MonoDroid(26699): at Xamarin.Forms.Platform.Android.VisualElementRenderer
1<Xamarin.Forms.View>.SetElement (Xamarin.Forms.View) <0x0050b>
07-14 13:52:01.276 I/MonoDroid(26699): at Xamarin.Forms.Platform.Android.VisualElementRenderer`1<Xamarin.Forms.View>.Xamarin.Forms.Platform.Android.IVisualElementRenderer.SetElement (Xamarin.Forms.VisualElement) <0x0012b>
07-14 13:52:01.276 I/MonoDroid(26699): at Xamarin.Forms.Platform.Android.ViewCellRenderer/ViewCellContainer.Update (Xamarin.Forms.ViewCell)
07-14 13:52:01.276 I/MonoDroid(26699): at Xamarin.Forms.Platform.Android.ViewCellRenderer.GetCellCore (Xamarin.Forms.Cell,Android.Views.View,Android.Views.ViewGroup,Android.Content.Context)
07-14 13:52:01.276 I/MonoDroid(26699): at Xamarin.Forms.Platform.Android.CellRenderer.GetCell (Xamarin.Forms.Cell,Android.Views.View,Android.Views.ViewGroup,Android.Content.Context)
07-14 13:52:01.276 I/MonoDroid(26699): at Xamarin.Forms.Platform.Android.CellFactory.GetCell (Xamarin.Forms.Cell,Android.Views.View,Android.Views.ViewGroup,Android.Content.Context,Xamarin.Forms.View)
07-14 13:52:01.276 I/MonoDroid(26699): at Xamarin.Forms.Platform.Android.ListViewAdapter.GetView (int,Android.Views.View,Android.Views.ViewGroup)
07-14 13:52:01.276 I/MonoDroid(26699): at Android.Widget.BaseAdapter.n_GetView_ILandroid_view_View_Landroid_view_ViewGroup_ (intptr,intptr,int,intptr,intptr) [0x0001a] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.12-series/163212a9/source/monodroid/src/Mono.Android/platforms/android-19/src/generated/Android.Widget.BaseAdapter.cs:460
07-14 13:52:01.276 I/MonoDroid(26699): at (wrapper dynamic-method) object.4fb7f83a-ae6b-4af9-b4ca-c350e9409cfc (intptr,intptr,int,intptr,intptr)
An unhandled exception occured.

07-14 13:52:02.917 W/dalvikvm(26699): JNI WARNING: JNI function NewString called with exception pending
07-14 13:52:02.917 W/dalvikvm(26699): in Lxamarin/forms/platform/android/ListViewAdapter;.n_getView:(ILandroid/view/View;Landroid/view/ViewGroup;)Landroid/view/View; (NewString)
07-14 13:52:02.917 W/dalvikvm(26699): Pending exception is:
07-14 13:52:02.927 I/dalvikvm(26699): java.lang.Throwable: System.ArgumentOutOfRangeException: Argument is out of range.
07-14 13:52:02.927 I/dalvikvm(26699): Parameter name: index
07-14 13:52:02.927 I/dalvikvm(26699): at System.Collections.Generic.List

1<Xamarin.Forms.Element>.get_Item (int) <0x0007c>
07-14 13:52:02.927 I/dalvikvm(26699): at System.Collections.ObjectModel.Collection
1<Xamarin.Forms.Element>.get_Item (int) <0x00083>
07-14 13:52:02.927 I/dalvikvm(26699): at System.Collections.ObjectModel.ReadOnlyCollection
1<Xamarin.Forms.Element>.get_Item (int) <0x00083>
07-14 13:52:02.927 I/dalvikvm(26699): at Xamarin.Forms.Platform.Android.VisualElementPackager.SetModel (Xamarin.Forms.VisualElement,Xamarin.Forms.VisualElement) <IL 0x0010b, 0x00947>
07-14 13:52:02.927 I/dalvikvm(26699): at Xamarin.Forms.Platform.Android.VisualElementPackager.<.ctor>b__0 (object,Xamarin.Forms.Platform.Android.VisualElementChangedEventArgs) <IL 0x0000d, 0x000a3>
07-14 13:52:02.927 I/dalvikvm(26699): at Xamarin.Forms.Platform.Android.VisualElementRenderer
1<Xamarin.Forms.View>.OnElementChanged (Xamarin.Forms.Platform.Android.ElementChangedEventArgs
1<Xamarin.Forms.View>) <0x00137>
07-14 13:52:02.927 I/dalvikvm(26699): at Xamarin.Forms.Platform.Android.VisualElementRenderer
1<Xamarin.Forms.View>.SetElement (Xamarin.Forms.View) <0x0050b>
07-14 13:52:02.927 I/dalvikvm(26699): at Xamarin.Forms.Platform.
07-14 13:52:02.927 I/dalvikvm(26699): at xamarin.forms.platform.android.ListViewAdapter.n_getView(Native Method)
07-14 13:52:02.927 I/dalvikvm(26699): at xamarin.forms.platform.android.ListViewAdapter.getView(ListViewAdapter.java:33)
07-14 13:52:02.927 I/dalvikvm(26699): at android.widget.AbsListView.obtainView(AbsListView.java:2255)
07-14 13:52:02.927 I/dalvikvm(26699): at android.widget.ListView.measureHeightOfChildren(ListView.java:1263)
In mgmain JNI_OnLoad
07-14 13:52:02.937 I/dalvikvm(26699): at android.widget.ListView.onMeasure(ListView.java:1175)
07-14 13:52:02.937 I/dalvikvm(26699): at android.view.View.measure(View.java:16497)
07-14 13:52:02.937 I/dalvikvm(26699): at xamarin.forms.platform.android.ViewRenderer_2.n_onMeasure(Native Method)
07-14 13:52:02.937 I/dalvikvm(26699): at xamarin.forms.platform.android.ViewRenderer_2.onMeasure(ViewRenderer_2.java:55)
07-14 13:52:02.937 I/dalvikvm(26699): at android.view.View.measure(View.java:16497)
07-14 13:52:02.937 I/dalvikvm(26699): at mono.android.app.TabEventDispatcher.n_onTabSelected(Native Method)
07-14 13:52:02.947 I/dalvikvm(26699): at mono.android.app.TabEventDispatcher.onTabSelected(TabEventDispatcher.java:39)
07-14 13:52:02.947 I/dalvikvm(26699): at com.android.internal.app.ActionBarImpl.selectTab(ActionBarImpl.java:577)
07-14 13:52:02.947 I/dalvikvm(26699): at com.android.internal.app.ActionBarImpl$TabImpl.select(ActionBarImpl.java:1105)
07-14 13:52:02.947 I/dalvikvm(26699): at com.android.internal.widget.ScrollingTabContainerView$TabClickListener.onClick(ScrollingTabContainerView.java:547)
07-14 13:52:02.947 I/dalvikvm(26699): at android.view.View.performClick(View.java:4438)
07-14 13:52:02.947 I/dalvikvm(26699): at android.view.View$PerformClick.run(View.java:18422)
07-14 13:52:02.947 I/dalvikvm(26699): at android.os.Handler.handleCallback(Handler.java:733)
07-14 13:52:02.947 I/dalvikvm(26699): at android.os.Handler.dispatchMessage(Handler.java:95)
07-14 13:52:02.947 I/dalvikvm(26699): at android.os.Looper.loop(Looper.java:136)
07-14 13:52:02.947 I/dalvikvm(26699): at android.app.ActivityThread.main(ActivityThread.java:5001)
07-14 13:52:02.947 I/dalvikvm(26699): at java.lang.reflect.Method.invokeNative(Native Method)
07-14 13:52:02.947 I/dalvikvm(26699): at java.lang.reflect.Method.invoke(Method.java:515)
07-14 13:52:02.947 I/dalvikvm(26699): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
07-14 13:52:02.947 I/dalvikvm(26699): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
07-14 13:52:02.957 I/dalvikvm(26699): at dalvik.system.NativeStart.main(Native Method)
07-14 13:52:02.957 I/dalvikvm(26699): "main" prio=5 tid=1 NATIVE
07-14 13:52:02.957 I/dalvikvm(26699): | group="main" sCount=0 dsCount=0 obj=0x415e3ca8 self=0x4151d408
07-14 13:52:02.957 I/dalvikvm(26699): | sysTid=26699 nice=-6 sched=0/0 cgrp=apps handle=1074491732
07-14 13:52:02.957 I/dalvikvm(26699): | state=R schedstat=( 0 0 0 ) utm=523 stm=70 core=0
07-14 13:52:02.967 I/dalvikvm(26699): #00 pc 0000132e /system/lib/libcorkscrew.so (unwind_backtrace_thread+29)
07-14 13:52:02.967 I/dalvikvm(26699): #01 pc 0006064e /system/lib/libdvm.so (dvmDumpNativeStack(DebugOutputTarget const*, int)+33)
07-14 13:52:02.967 I/dalvikvm(26699): #02 pc 0005463c /system/lib/libdvm.so (dvmDumpThreadEx(DebugOutputTarget const*, Thread*, bool)+395)
The program 'Mono' has exited with code 0 (0x0).

Here's my renderer

public class FontAwesomeRenderer : LabelRenderer
{
    const string FontName = @"fontawesome-webfont.ttf";

    protected override void OnElementChanged(ElementChangedEventArgs<Label> e)
    {
        base.OnElementChanged(e);
        if (e.OldElement != null) return;

        var view = (FontAwesomeLabel) Element;
        UpdateUI(view, Control);
    }

    protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        base.OnElementPropertyChanged(sender, e);
        if (e.PropertyName != FontAwesomeLabel.CommandProperty.PropertyName) return;

        var view = (FontAwesomeLabel)Element;
        AddGestureRecognizer(view);
    }

    private static void AddGestureRecognizer(FontAwesomeLabel element)
    {
        if (element.Command == null) return;

        var tgr = new TapGestureRecognizer();
        tgr.Tapped += (sender, args) =>
        {
            if (element.Command.CanExecute(element.CommandParameter))
                element.Command.Execute(element.CommandParameter);
        };

        element.GestureRecognizers.Add(tgr);
    }

    private void UpdateUI(FontAwesomeLabel view, TextView control)
    {
        control.Typeface = TrySetFont(FontName);

        if (view.FontSize > 0)
            control.TextSize = (float)view.FontSize;
    }

    private Typeface TrySetFont(string fontName)
    {
        Typeface typeface;
        try
        {
            typeface = Typeface.CreateFromAsset(Context.Assets, fontName);
            return typeface;
        }
        catch (Exception ex)
        {
            Console.Write(@"font not found in assets {0}", ex);
            try
            {
                typeface = Typeface.CreateFromFile(fontName);
                return typeface;
            }
            catch (Exception ex1)
            {
                Console.Write(ex1);
                return Typeface.Default;
            }
        }
    }
}

and the code I'm using to display

       var headerTemplate = new DataTemplate(typeof(ModuleMediaListHeaderTemplate));
        headerTemplate.CreateContent();

        var itemTemplate = new DataTemplate(typeof(ModuleMediaListItemTemplate));
        itemTemplate.CreateContent();

        _listView = new ListView
        {
            ItemsSource = viewModel.MediaSections,
            IsGroupingEnabled = true,
            GroupDisplayBinding = new Binding("SectionName"),
            HasUnevenRows = false,
            GroupHeaderTemplate = headerTemplate,
            ItemTemplate = itemTemplate
        };

public class ModuleMediaListItemTemplate : ViewCell
{
    public ModuleMediaListItemTemplate()
    {
        var icon = new FontAwesomeLabel { FontSize = 18, YAlign = TextAlignment.Center };
        icon.SetBinding(Label.TextProperty, "Icon");

        var title = new Label{ YAlign = TextAlignment.Center};
        title.SetBinding(Label.TextProperty, new Binding("Title", BindingMode.Default, new UppercaseValueConverter()));

        View = new StackLayout
        {
            Padding = new Thickness(8, 8, 8, 8),
            Orientation = StackOrientation.Horizontal,
            Children = {icon, title}
        };
    }
}

Viewing all articles
Browse latest Browse all 58056

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>