小编Sur*_*agi的帖子

自定义SurfaceView导致NoSuchMethodException

我有一个自定义View扩展SurfaceView.XML布局是

<com.myPackage.MyCustomView
  android:id="@+id/mycview"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"/>
Run Code Online (Sandbox Code Playgroud)

这堂课是:

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;


     }
}
Run Code Online (Sandbox Code Playgroud)

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

MyCustomView mv = (MyCustomView)findViewById(R.id.mycview);
Run Code Online (Sandbox Code Playgroud)

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)
Run Code Online (Sandbox Code Playgroud)

由于某种原因,我的构造函数导致上述异常.我很感激任何帮助找到代码的错误.

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

 XmlPullParser parser = getResources().getXml(R.id.mycview);
 AttributeSet attributes = Xml.asAttributeSet(parser);


 MyCustomView cv = new MyCustomView(this,attributes,xx,yy,uu);
              cv = …
Run Code Online (Sandbox Code Playgroud)

xml android constructor exception custom-view

4
推荐指数
1
解决办法
3727
查看次数

标签 统计

android ×1

constructor ×1

custom-view ×1

exception ×1

xml ×1