简体   繁体   English

构造函数未被调用

[英]Constructors not being called

I'm creating a custom View that requires a few flags to be set in the constructors. 我正在创建一个自定义View ,该View需要在构造函数中设置一些标志。 The flags are not set anywhere else, and are saved/restored in the onSaveInstanceState and onRestoreInstanceState methods. 这些标志未在其他任何地方设置,而是在onSaveInstanceStateonRestoreInstanceState方法中保存/恢复。

The problem is when I try to use it, the flags are not always set. 问题是,当我尝试使用它时,并非总是设置标志。 I logged which constructor is used for each instance, but it seems there are cases where none of them are used. 我记录了每个实例使用哪个构造函数,但是似乎在某些情况下没有使用它们。 I can't think of any other constructor that could be getting called instead. 我想不出可以调用任何其他构造函数。

public class TextViewCompat extends AppCompatTextView
{
    private int constructor = 0;
    private boolean isPreHoneycomb;
    private boolean isPreJellybean;

    public TextViewCompat(Context context)
    {
        this(context, null);
        constructor = 1;
    }

    public TextViewCompat(Context context, AttributeSet attrs)
    {
        this(context, attrs, 0);
        constructor = 2;
    }

    public TextViewCompat(Context context, AttributeSet attrs, int defStyle)
    {
        super(context, attrs, defStyle);

        isPreHoneycomb = Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB;
        isPreJellybean = Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1;

        constructor = 3;
    }

    @Override
    public Parcelable onSaveInstanceState()
    {
        Log.d("Kevin", "onSaveInstanceState");
        Bundle bundle = new Bundle();
        bundle.putParcelable("instanceState", super.onSaveInstanceState());
        bundle.putBoolean("isPreHoneycomb", isPreHoneycomb);
        bundle.putBoolean("isPreJellybean", isPreJellybean);

        return bundle;
    }

    @Override
    public void onRestoreInstanceState(Parcelable state)
    {
        Log.d("Kevin", "onRestoreInstanceState");
        if (state instanceof Bundle)
        {
            Bundle bundle = (Bundle) state;
            isPreHoneycomb = bundle.getBoolean("isPreHoneycomb", Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB);
            isPreJellybean = bundle.getBoolean("isPreJellybean", Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1);

            state = bundle.getParcelable("instanceState");
        }
        super.onRestoreInstanceState(state);
    }

    @Override
    protected void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter)
    {    
        Log.d("Kevin", "=================");
        Log.d("Kevin", "constructor=" + constructor);
        Log.d("Kevin", "isPreJellybean=" + isPreJellybean);
        Log.d("Kevin", "isPreHoneycomb=" + isPreHoneycomb);
        Log.d("Kevin", "=================");

        super.onTextChanged(text, start, lengthBefore, lengthAfter);
    }
}

I know that it would be pretty easy for me to work around this issue in this case, because the flags can be recalculated at any time. 我知道在这种情况下,对我来说很容易解决此问题,因为可以随时重新计算标志。 But the point is that the constructors aren't called and would be an issue for any other flag I'd like to keep. 但是关键是没有调用构造函数,这对我想保留的其他任何标志都是一个问题。

Note: The views are inflated from xml instead of programatically, and testing on an API 10 emulator and device (both flags should be true). 注意:视图是从xml(而非以编程方式)膨胀的,并在API 10仿真器和设备上进行测试(两个标志均应为true)。

You can try this .Pass super instead of this 你可以试试这个.Pass super代替this

public TextViewCompat(Context context)
{
    super(context);
    constructor = 1;
}

public TextViewCompat(Context context, AttributeSet attrs)
{
    super(context, attrs);
    constructor = 2;
}

I found the issue. 我发现了问题。 I'm printing out the flags in the onTextChanged method. 我正在打印onTextChanged方法中的标志。 Since I'm inflating the view from the layout (text set in the attrs), the constructor is setting the text on the line super(context, attrs, defStyle); 由于我是从布局(在attrs中设置的文本)放大视图,因此构造函数在super(context, attrs, defStyle);行上设置了文本super(context, attrs, defStyle); and firing onTextChanged before the flags are set. 并在设置标志之前触发onTextChanged

Silly mistake, but thanks for any of the comments that nudged me in the correct direction. 愚蠢的错误,但感谢您向正确的方向推动了我的任何评论。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM