导致NoSuchMethodException的自定义SurfaceView(Custom SurfaceView causing NoSuchMethodException)

编程入门 行业动态 更新时间:2024-10-24 18:21:40
导致NoSuchMethodException的自定义SurfaceView(Custom SurfaceView causing NoSuchMethodException)

我有一个自定义视图扩展SurfaceView。 XML布局是

<com.myPackage.MyCustomView

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

这个班是:

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; } ... }

在此方法的上下文Activity中,我有以下行

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

LogCat输出具有以下内容

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)

出于某种原因,我的构造函数导致这个异常。 我将不胜感激任何帮助找到代码有问题。

更新:我改变了构造函数来添加AttributeSet,并在我的活动中写下了以下内容:

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);

但是我得到相同的logcat输出。

I have a custom View extending SurfaceView. The XML layout is

<com.myPackage.MyCustomView android:id="@+id/mycview" android:layout_width="fill_parent" android:layout_height="fill_parent"/>

The class is :

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 context Activity for this method, I have the following line

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

The 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 is causing above exception. I would appreciate any help finding what is wrong with the code.

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

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.

最满意答案

您没有正确的构造函数MyCustomView(Context,AttributeSet)

如果您想膨胀视图并在代码中创建新视图,则必须创建以下构造函数。 使用initYourStuff()来初始化你的东西;),你当然也可以参数化它们......

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(); }

You don't have the correct constructor MyCustomView(Context,AttributeSet)

You must create the following constructors if you want to inflate views, and create new one in code. use initYourStuff() to init your stuff ;) , you can also parametrize 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(); }

更多推荐

本文发布于:2023-08-04 09:24:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1414806.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自定义   NoSuchMethodException   SurfaceView   causing   Custom

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!