自定义 SurfaceView 导致 NoSuchMethodException

发布于 2024-12-29 18:56:40 字数 1566 浏览 4 评论 0原文

我有一个扩展 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

各空 2025-01-05 18:56:40

您没有正确的构造函数 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();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文