Quantcast
Channel: Recent Threads — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 204402

Another Exception of inflate Custom View with custom state

$
0
0

I want to define a custom view with custom state. The code are as follows.

attrs.xml:

        <?xml version="1.0" encoding="utf-8"?>
        <resources>
          <declare-styleable name="TabPage">
            <attr name="tab_state_selected" format="boolean"/>
          </declare-styleable>
        </resources>

TabPage.cs:

namespace Sfi.Fw.Ui.Tab
{
    public class TabPage : TextView
    {

        public TabPage(Context context) : this(context, null)
        {

        }

        public TabPage(Context context, IAttributeSet attrs) : this(context, attrs, 0)
        {

        }

        public TabPage(Context context, IAttributeSet attrs, int defStyle) : base(context, attrs, defStyle)
        {
            TypedArray a = context.ObtainStyledAttributes(attrs, Resource.Styleable.TabPage, defStyle, 0);
            tabSelected = a.GetBoolean(Resource.Styleable.TabPage_tab_state_selected, false);
            a.Recycle();
        }

        private bool tabSelected = false;

        static readonly int[] STATE_TAB_SELECTED = { Resource.Attribute.tab_state_selected };

        protected override int[] OnCreateDrawableState(int extraSpace)
        {
            var drawableStates = base.OnCreateDrawableState(extraSpace + 1);

            if (tabSelected)
            {
                MergeDrawableStates(drawableStates, STATE_TAB_SELECTED);
                return drawableStates;
            }

            return drawableStates;
        }


        public bool IsTabSelected
        {
            get { return tabSelected; }
            set
            {
                if (tabSelected != value)
                {
                    tabSelected = value;
                    RefreshDrawableState();
                }
            }
        }
    }
}

Main.axml:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:custom="http://schemas.android.com/apk/res/Sfi.Fw"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
          <Sfi.Fw.Ui.Tab.TabPage
              custom:tab_state_selected="true"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="Test Tab Item"
              />
    </LinearLayout>

MainActivity.cs:

    public class MainActivity : Activity
    {

        protected override void OnCreate (Bundle bundle)
        {
        base.OnCreate (bundle);

            SetContentView (Resource.Layout.Main);
        }
    }

when I start debug, vs give me the exception below

        0x21 in System.Diagnostics.Debugger.Mono_UnhandledException_internal    C#
    0x1 in System.Diagnostics.Debugger.Mono_UnhandledException  C#
    0x27 in object.c2388932-21f5-4e89-af4f-333d34069a7f C#
    0x254 in Java.Interop.TypeManager.CreateInstance at /Users/builder/data/lanes/monodroid-mlion-monodroid-4.10.2-branch/4b53fbd0/source/monodroid/src/Mono.Android/src/Java.Interop/TypeManager.cs:253    C#
    0xBD in Java.Lang.Object.GetObject at /Users/builder/data/lanes/monodroid-mlion-monodroid-4.10.2-branch/4b53fbd0/source/monodroid/src/Mono.Android/src/Java.Lang/Object.cs:301  C#
    0x26 in Java.Lang.Object._GetObject<Android.Views.View> at /Users/builder/data/lanes/monodroid-mlion-monodroid-4.10.2-branch/4b53fbd0/source/monodroid/src/Mono.Android/src/Java.Lang/Object.cs:281 C#
    0x2 in Java.Lang.Object.GetObject<Android.Views.View> at /Users/builder/data/lanes/monodroid-mlion-monodroid-4.10.2-branch/4b53fbd0/source/monodroid/src/Mono.Android/src/Java.Lang/Object.cs:273   C#
    0x8 in Java.Lang.Object.GetObject<Android.Views.View> at /Users/builder/data/lanes/monodroid-mlion-monodroid-4.10.2-branch/4b53fbd0/source/monodroid/src/Mono.Android/src/Java.Lang/Object.cs:267   C#
    0x3 in Android.Views.View.n_OnCreateDrawableState_I at /Users/builder/data/lanes/monodroid-mlion-monodroid-4.10.2-branch/4b53fbd0/source/monodroid/src/Mono.Android/platforms/android-14/src/generated/Android.Views.View.cs:10652  C#
    0x17 in object.c2388932-21f5-4e89-af4f-333d34069a7f C#
    0x73 in Android.Runtime.JNIEnv.CallNonvirtualVoidMethod at /Users/builder/data/lanes/monodroid-mlion-monodroid-4.10.2-branch/4b53fbd0/source/monodroid/src/Mono.Android/src/Runtime/JNIEnv.g.cs:891 C#
    0x99 in Android.App.Activity.SetContentView at /Users/builder/data/lanes/monodroid-mlion-monodroid-4.10.2-branch/4b53fbd0/source/monodroid/src/Mono.Android/platforms/android-14/src/generated/Android.App.Activity.cs:3830 C#
>   0x1C in Sfi.Fw.MainActivity.OnCreate at c:\Users\Public\Documents\Workspace\BE\SVN\Projects\SFI.FW.Android\SFI.FW.Android\MainActivity.cs:55    C#
    0x13 in Android.App.Activity.n_OnCreate_Landroid_os_Bundle_ at /Users/builder/data/lanes/monodroid-mlion-monodroid-4.10.2-branch/4b53fbd0/source/monodroid/src/Mono.Android/platforms/android-14/src/generated/Android.App.Activity.cs:1943 C#
    0x17 in object.d39d305a-95bf-4720-a160-1d4029d95aa9 C#

I want to know the reason for this exception. Thank you very much.


Viewing all articles
Browse latest Browse all 204402

Trending Articles



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