使用extends时抛出Android NoSuchMethodException

时间:2014-05-14 07:49:38

标签: android extends nosuchmethod

我有一个Activity CropActivity,它使用我自己编写的View CropView。 CropView扩展了SelectorView(也是自写),SelectorView扩展了ImageView。

启动CropActivity时,抛出NoSuchMethodException。 当使用SelectorView作为CropActivity时,不会抛出任何错误,任何想法?

Cropview类 公共类CropView扩展了SelectorView {

public CropView(Context context) {
    super(context);
    initCropView();
}

private void initCropView() {
    setmPaintColor(Color.WHITE);
    setsMinimumSize(metrics.densityDpi);
    setsTouchBuffer(metrics.densityDpi / 3);
}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    if (getmLeftTop().equals(0, 0))
        resetPoints();
    // draw the points on the screen; one in every corner and one in the center
    canvas.drawRect(getmLeftTop().x, getmLeftTop().y, getmRightBottom().x, getmRightBottom().y,
            getmPaint());
    canvas.drawCircle(getmLeftTop().x, getmLeftTop().y, 40, getmPaint());
    canvas.drawCircle(getmLeftTop().x, getmRightBottom().y, 40, getmPaint());
    canvas.drawCircle(getmRightBottom().x, getmLeftTop().y, 40, getmPaint());
    canvas.drawCircle(getmRightBottom().x, getmRightBottom().y, 40, getmPaint());
    canvas.drawCircle(getmCenter().x, getmCenter().y, 10, getmPaint());
}

Selectorview代码段

public SelectorView(Context context) {
    super(context);
    initSelectorView();
}

public SelectorView(Context context, AttributeSet attrs) {
    super(context, attrs, 0);
    initSelectorView();
}

public SelectorView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    initSelectorView();
}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
}

/**
 * Initializes the cropview and variables.
 */
private void initSelectorView() {
    mPaint.setStyle(Style.STROKE);
    mPaint.setStrokeWidth(5);
    mLeftTop = new Point();
    mRightBottom = new Point();
    mCenter = new Point();
    mScreenCenter = new Point();
    mPrevious = new Point();
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    int eventaction = event.getAction();
    switch (eventaction) {
    // set the touch point
        case MotionEvent.ACTION_DOWN:
            mPrevious.set((int) event.getX(), (int) event.getY());
            break;

从CropActivity创建

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_crop);

    mContext = this;
    mCropView = new CropView(this);
    mFile = new File(getIntent().getStringExtra("imgpath"));
    mBitmaps = new ArrayList<Bitmap>();
    mTasks = new ArrayList();
    mNumberOfCores = Runtime.getRuntime().availableProcessors();
    mProgressDialog = new ProgressDialog(mContext);
    mProgressDialog.setTitle("Enhancing image");
    mBitmapDrawable = null;
    mBitmapDrawable = (BitmapDrawable) Drawable.createFromPath(mFile.getAbsolutePath());

    mCropView = (CropView) findViewById(R.id.image_preview);
    mCropView.setImageDrawable(mBitmapDrawable);

    mCropButton = (Button) findViewById(R.id.button_crop);

1 个答案:

答案 0 :(得分:0)

通过XML膨胀的自定义视图需要以下构造函数:

ViewName(Context)
ViewName(Context, AttributeSet)
ViewName(Context, AttributeSet, int)

你只有第一个,我想这个例外是关于CropView中缺少的第二个。