CustomButton android, переопределяя OnCreateDrawableState, вызывает сбой приложения - PullRequest
0 голосов
/ 19 октября 2018

Я создал пользовательскую кнопку, унаследованную от RelativeLayout, я добавил к этой кнопке пользовательское состояние «state_full_version».

Когда я ovveride OnCreateDrawableState, приложение вылетало со следующим сообщением:

enter image description here

Найдите ниже мой код CustomButton:

  <declare-styleable name="CustomButton">
<attr name="android:text" />
<attr name="android:textColor" />
<attr name="android:textSize" />
<attr name="android:drawable" />
<attr name="android:padding" />
<attr name="onlyInFullVersion" format="boolean" />
<attr name="state_full_version" format="boolean" />

и код c #

public class CustomButton : RelativeLayout
{
    public CustomButton(Context context) : base(context, null)
    {
        this.Initialize(context, null);
    }

    public CustomButton(Context context, IAttributeSet attrs) : base(context, attrs)
    {
        this.Initialize(context, attrs);
    }

    public CustomButton(Context context, IAttributeSet attrs, int defStyle) : base(context, attrs, defStyle)
    {
        this.Initialize(context, attrs);
    }
    public string Text
    {
        get => this._textView.Text;
        set => this._textView.Text = value;
    }

    private bool _onlyInFullVersion;
    private TextView _textView;
    private void Initialize(Context context, IAttributeSet attrs)
    {
        string text = string.Empty;
        Color textColor = Color.White;
        Drawable image = null;
        int textSize = 0;
        int padding = 0;
        if (attrs != null)
        {
            TypedArray a = this.Context.ObtainStyledAttributes(attrs, Resource.Styleable.CustomButton);
            text = a.GetString(Resource.Styleable.CustomButton_android_text);
            textColor = a.GetColor(Resource.Styleable.CustomButton_android_textColor, 0);
            textSize = a.GetDimensionPixelSize(Resource.Styleable.CustomButton_android_textSize, 0);
            image = a.GetDrawable(Resource.Styleable.CustomButton_android_drawable);
            padding = a.GetDimensionPixelSize(Resource.Styleable.CustomButton_android_padding, 0);
            this._onlyInFullVersion = a.GetBoolean(Resource.Styleable.CustomButton_onlyInFullVersion, false);
            a.Recycle();
        }

        this.Clickable = true;
        LinearLayout linearLayout = new LinearLayout(context, null);
        linearLayout.Orientation = Orientation.Horizontal;
        ImageView imageView = new ImageView(context);
        if (image != null)
            imageView.SetImageDrawable(image);
        imageView.SetAdjustViewBounds(true);
        imageView.SetPadding(padding, padding, padding, padding);
        imageView.SetScaleType(ImageView.ScaleType.CenterInside);
        LayoutParams layoutParamsImage = new LayoutParams(ViewGroup.LayoutParams.WrapContent, ViewGroup.LayoutParams.MatchParent);
        layoutParamsImage.AddRule(LayoutRules.CenterVertical);
        linearLayout.AddView(imageView, layoutParamsImage);

        this._textView = new TextView(context, null);
        this._textView.Typeface = UiHelper.FontRegular;
        this._textView.Text = text;
        this._textView.SetTextSize(ComplexUnitType.Px, textSize);
        this._textView.SetTextColor(textColor);
        this._textView.Gravity = GravityFlags.CenterVertical;
        this._textView.SetPadding(padding, padding, padding, padding);

        LayoutParams layoutParams = new LayoutParams(ViewGroup.LayoutParams.WrapContent, ViewGroup.LayoutParams.MatchParent);
        layoutParams.AddRule(LayoutRules.CenterVertical);
        linearLayout.AddView(this._textView, layoutParams);

        LayoutParams layoutParamsLinearLayout = new LayoutParams(ViewGroup.LayoutParams.WrapContent, ViewGroup.LayoutParams.MatchParent);
        layoutParamsLinearLayout.AddRule(LayoutRules.CenterVertical);
        layoutParamsLinearLayout.AddRule(LayoutRules.CenterHorizontal);
        this.AddView(linearLayout, layoutParamsLinearLayout);

        if (this._onlyInFullVersion && !InAppManager.IsFullVersion)
        {
            this.Background.SetState(new[] { Resource.Attribute.state_full_version });
            this.RefreshDrawableState();
        }
    }

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

        if (this._onlyInFullVersion && InAppManager.IsFullVersion)
            MergeDrawableStates(drawableState, new[] { Resource.Attribute.state_full_version });
        return (drawableState);
    }
}

код работает, если я комментирую переопределение OnCreateDrawableState.

Что я сделал не так?

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...