Parent does not contain a constructor that takes 0 arguments

天大地大妈咪最大 提交于 2019-12-13 05:10:41

问题


My code is

 namespace classlibrary
 {
   public class numerictext : EditText
    {
      //My code

    } 

When I try to inherit a edit text control in a class library, I'm getting the error: Parent does not contain a constructor that takes 0 arguments. I understand the problem is that Parent has no constructor with 0 arguments. But how to inherit a control in a class library?


回答1:


You need to call one of the base constructors in your derived class. Assuming you are using the Android EditText widget:

public class numerictext : EditText
{
    public numerictext(Context context) : base(context)
                                      //^^^^^^^^^^^^^^^ 
                                      //Note how we are now calling the base constructure
    {
        //empty or you can add your own code
    }

    public numerictext(Context context, IAttributeSet attrs) : base(context, attrs)
    {
    }

    //etc
} 



回答2:


For any Android-based View subclass, you need to supply the three constructors that call their base object constructors, ie.:

public class MyView : EditText
{
    public MyView(Context context) : base(context) { }
    public MyView(Context context, IAttributeSet attrs) : base(context, attrs) { }
    public MyView(Context context, IAttributeSet attrs, int defStyle) : base(context, attrs, defStyle) { }

    // your override code....
}



回答3:


Have you considered changing EditText to:

public class EditTtext 
{
   data stuff..
   EditText() {}

   other functions
}

Though I don't see why yours wouldn't work as is



来源:https://stackoverflow.com/questions/51233923/parent-does-not-contain-a-constructor-that-takes-0-arguments

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!