Custom SurfaceView throwing NoSuchMethodException

I have a custom view extending SurfaceView. XML Layout

<com.myPackage.MyCustomView 

android: id = "@ + id / mycview" android: layout_width = "fill_parent" android: layout_height = "fill_parent" / ">

Grade:

  public class MyCustomView extends SurfaceView{ public float[] xpositions; public float[] ypositions; public String[] units; public MyCustomView(Context context, float[] xpos, float[] ypos,String[] u) { super(context); xpositions=xpos; ypositions =ypos; units=u; } ... } 

In the Activity context for this method, I have the following line

 MyCustomView mv = (MyCustomView)findViewById(R.id.mycview); 

LogCat output has the following

 01-30 01:51:12.124: ERROR/AndroidRuntime(4934): Caused by: java.lang.NoSuchMethodException:MyCustomView(Context,AttributeSet) 01-30 01:51:12.124: ERROR/AndroidRuntime(4934): at java.lang.Class.getMatchingConstructor(Class.java:674) 01-30 01:51:12.124: ERROR/AndroidRuntime(4934): at java.lang.Class.getConstructor(Class.java:486) 01-30 01:51:12.124: ERROR/AndroidRuntime(4934): at android.view.LayoutInflater.createView(LayoutInflater.java:475) 

For some reason, my constructor raises this exception. I would appreciate any help finding out what is wrong with the code.

UPDATE: I changed the constructor to add an AttributeSet, and wrote the following in my activity:

  XmlPullParser parser = getResources().getXml(R.id.mycview); AttributeSet attributes = Xml.asAttributeSet(parser); MyCustomView cv =new MyCustomView(this,attributes,xx,yy,uu); cv= (MyCustomView)findViewById(R.id.mycview); 

But I get the same logcat output.

+4
source share
1 answer

You do not have the correct constructor MyCustomView (Context, AttributeSet)

You must create the following constructors if you want to inflate views and create new code. use initYourStuff() to start your stuff;), you can also parameterize them, of course ...

 public MyCustomView(Context context) { super(context); this.context = context; initYourStuff(); } public MyCustomView(Context context, AttributeSet attrs) { super(context, attrs); this.context = context; initYourStuff(); } public MyCustomView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); this.context = context; initYourStuff(); } 
+16
source

All Articles