0

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

启动 CropActivity 时,会引发 NoSuchMethodException。为 CropActivity 使用 SelectorView 时,没有抛出错误,有什么想法吗?

Cropview 类 public class 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());
}

选择视图片段

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 的 oncreate

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

1 回答 1

0

Custom views inflated via XML need the following constructors:

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

You only have the first one and I guess the exception is about the second one being missing in your CropView.

于 2014-05-14T08:10:54.783 回答