自定义SurfaceView造成NoSuchMethodException [英] Custom SurfaceView causing NoSuchMethodException

查看:151
本文介绍了自定义SurfaceView造成NoSuchMethodException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有延伸SurfaceView自定义视图。该XML布局

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

<com.myPackage.MyCustomView

机器人:ID =@ + ID / mycview   机器人:layout_width =FILL_PARENT   机器人:layout_height =FILL_PARENT    />

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;



}
    ...
    }

在此方法,我有以下线的范围内活动。

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

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

在LogCat中输出具有以下

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)

出于某种原因,我的构造导致此异常。我想AP preciate任何帮助寻找什么是错的code。

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

更新: 我改变了构造函数中添加的AttributeSet和我的活动中写道:

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

不过,我得到相同的logcat输出。

But I get the same logcat output.

推荐答案

您没有正确的构造MyCustomView(背景下,AttributeSet中)

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

如果你想夸大的观点,您必须创建下面的构造函数,并创造新的code。 使用 initYourStuff()来初始化你的东西),你也可以参数化他们当然...

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

这篇关于自定义SurfaceView造成NoSuchMethodException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆