0

我创建了一个视图,我想像自定义小部件一样使用它来在我的布局中绘制。但是一开始我有这个错误,我可以解决它。所以请帮忙!

这是我的主要布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <com.example.SignatureView
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

这是我的 SignatureView 类:

public class SignatureView extends View {

      private static final float STROKE_WIDTH = 5f;

      /** Need to track this so the dirty region can accommodate the stroke. **/
      private static final float HALF_STROKE_WIDTH = STROKE_WIDTH / 2;

      private Paint paint = new Paint();
      private Path path = new Path();

      /**
       * Optimizes painting by invalidating the smallest possible area.
       */
      private float lastTouchX;
      private float lastTouchY;
      private final RectF dirtyRect = new RectF();

      public SignatureView(Context context, AttributeSet attrs, int background) {
        super(context, attrs);
        setBackgroundResource(background);
        paint.setAntiAlias(true);
        paint.setColor(Color.BLACK);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeJoin(Paint.Join.ROUND);
        paint.setStrokeWidth(STROKE_WIDTH);

      }

      public void setColor(int color){
          paint.setColor(color);
      }

      /**
       * Erases the signature.
       */
      public void clear() {
        path.reset();

        // Repaints the entire view.
        invalidate();
      }

      @Override
      protected void onDraw(Canvas canvas) {
        canvas.drawPath(path, paint);
      }

      @Override
      public boolean onTouchEvent(MotionEvent event) {
        float eventX = event.getX();
        float eventY = event.getY();

        switch (event.getAction()) {
          case MotionEvent.ACTION_DOWN:
            path.moveTo(eventX, eventY);
            lastTouchX = eventX;
            lastTouchY = eventY;
            // There is no end point yet, so don't waste cycles invalidating.
            return true;

          case MotionEvent.ACTION_MOVE:
          case MotionEvent.ACTION_UP:
            // Start tracking the dirty region.
            resetDirtyRect(eventX, eventY);

            // When the hardware tracks events faster than they are delivered, the
            // event will contain a history of those skipped points.
            int historySize = event.getHistorySize();
            for (int i = 0; i < historySize; i++) {
              float historicalX = event.getHistoricalX(i);
              float historicalY = event.getHistoricalY(i);
              expandDirtyRect(historicalX, historicalY);
              path.lineTo(historicalX, historicalY);
            }

            // After replaying history, connect the line to the touch point.
            path.lineTo(eventX, eventY);
            break;

          default:
//          Log.("Ignored touch event: " + event.toString());
            return false;
        }

        // Include half the stroke width to avoid clipping.
        invalidate(
            (int) (dirtyRect.left - HALF_STROKE_WIDTH),
            (int) (dirtyRect.top - HALF_STROKE_WIDTH),
            (int) (dirtyRect.right + HALF_STROKE_WIDTH),
            (int) (dirtyRect.bottom + HALF_STROKE_WIDTH));

        lastTouchX = eventX;
        lastTouchY = eventY;

        return true;
      }

      /**
       * Called when replaying history to ensure the dirty region includes all
       * points.
       */
      private void expandDirtyRect(float historicalX, float historicalY) {
        if (historicalX < dirtyRect.left) {
          dirtyRect.left = historicalX;
        } else if (historicalX > dirtyRect.right) {
          dirtyRect.right = historicalX;
        }
        if (historicalY < dirtyRect.top) {
          dirtyRect.top = historicalY;
        } else if (historicalY > dirtyRect.bottom) {
          dirtyRect.bottom = historicalY;
        }
      }

      /**
       * Resets the dirty region when the motion event occurs.
       */
      private void resetDirtyRect(float eventX, float eventY) {

        // The lastTouchX and lastTouchY were set when the ACTION_DOWN
        // motion event occurred.
        dirtyRect.left = Math.min(lastTouchX, eventX);
        dirtyRect.right = Math.max(lastTouchX, eventX);
        dirtyRect.top = Math.min(lastTouchY, eventY);
        dirtyRect.bottom = Math.max(lastTouchY, eventY);
      }
    }

这就是我在主要 Draw 类中的内容:

 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        }

最后这就是我在 logCat 中得到的

02-19 17:41:51.708: E/AndroidRuntime(7530): FATAL EXCEPTION: main
02-19 17:41:51.708: E/AndroidRuntime(7530): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example/com.example.Draw}: android.view.InflateException: Binary XML file line #6: Error inflating class com.example.SignatureView
02-19 17:41:51.708: E/AndroidRuntime(7530):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1662)
02-19 17:41:51.708: E/AndroidRuntime(7530):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1678)
02-19 17:41:51.708: E/AndroidRuntime(7530):     at android.app.ActivityThread.access$1500(ActivityThread.java:118)
02-19 17:41:51.708: E/AndroidRuntime(7530):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:932)
02-19 17:41:51.708: E/AndroidRuntime(7530):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-19 17:41:51.708: E/AndroidRuntime(7530):     at android.os.Looper.loop(Looper.java:130)
02-19 17:41:51.708: E/AndroidRuntime(7530):     at android.app.ActivityThread.main(ActivityThread.java:3698)
02-19 17:41:51.708: E/AndroidRuntime(7530):     at java.lang.reflect.Method.invokeNative(Native Method)
02-19 17:41:51.708: E/AndroidRuntime(7530):     at java.lang.reflect.Method.invoke(Method.java:507)
02-19 17:41:51.708: E/AndroidRuntime(7530):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:875)
02-19 17:41:51.708: E/AndroidRuntime(7530):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:633)
02-19 17:41:51.708: E/AndroidRuntime(7530):     at dalvik.system.NativeStart.main(Native Method)
02-19 17:41:51.708: E/AndroidRuntime(7530): Caused by: android.view.InflateException: Binary XML file line #6: Error inflating class com.example.SignatureView
02-19 17:41:51.708: E/AndroidRuntime(7530):     at android.view.LayoutInflater.createView(LayoutInflater.java:508)
02-19 17:41:51.708: E/AndroidRuntime(7530):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:570)
02-19 17:41:51.708: E/AndroidRuntime(7530):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
02-19 17:41:51.708: E/AndroidRuntime(7530):     at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
02-19 17:41:51.708: E/AndroidRuntime(7530):     at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
02-19 17:41:51.708: E/AndroidRuntime(7530):     at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
02-19 17:41:51.708: E/AndroidRuntime(7530):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:207)
02-19 17:41:51.708: E/AndroidRuntime(7530):     at android.app.Activity.setContentView(Activity.java:1657)
02-19 17:41:51.708: E/AndroidRuntime(7530):     at com.example.Draw.onCreate(Draw.java:13)
02-19 17:41:51.708: E/AndroidRuntime(7530):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-19 17:41:51.708: E/AndroidRuntime(7530):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1626)
02-19 17:41:51.708: E/AndroidRuntime(7530):     ... 11 more
02-19 17:41:51.708: E/AndroidRuntime(7530): Caused by: java.lang.NoSuchMethodException: SignatureView(Context,AttributeSet)
02-19 17:41:51.708: E/AndroidRuntime(7530):     at java.lang.Class.getMatchingConstructor(Class.java:643)
02-19 17:41:51.708: E/AndroidRuntime(7530):     at java.lang.Class.getConstructor(Class.java:472)
02-19 17:41:51.708: E/AndroidRuntime(7530):     at android.view.LayoutInflater.createView(LayoutInflater.java:480)
02-19 17:41:51.708: E/AndroidRuntime(7530):     ... 21 more
4

3 回答 3

4

该错误非常自我解释:

Caused by: java.lang.NoSuchMethodException: SignatureView(Context,AttributeSet)

你没有一个只需要一个上下文和一个属性集的构造器。将该构造函数添加到您的 SignatureView 类,如下所示:

public SignatureView(Context c, AttributeSet as){
    super(c, as);
}
于 2013-02-19T16:47:26.490 回答
1
02-19 17:41:51.708: E/AndroidRuntime(7530): Caused by: java.lang.NoSuchMethodException: ***SignatureView(Context,AttributeSet)***

看起来你缺少一个构造函数?尝试为 SignatureView(Context,AttributeSet) 添加一个构造函数,看看它是如何工作的。

而不是传入int backgroundte 构造函数,尝试将其作为 xml 属性传递。这样,您仍然在回复默认构造函数,它使您可以灵活地从 xml 设置背景。

于 2013-02-19T16:48:41.030 回答
1

您需要一个接受 Context 和 AttributeSet 的构造函数。您向您的参数添加了第三个参数。通过 xml 进行膨胀时,这将不起作用,它需要确切的签名才能工作。

此外,这会使事情变得更加混乱,因为如果应用了默认样式,那么 xml inflater 会查找 3 参数版本(第三个是主题的资源 id),这会错误地调用您当前的 3 参数方法。最终结果将是您尝试设置随机主题 ID 的背景资源,我不知道会产生什么结果,但我的猜测是崩溃。

于 2013-02-19T16:46:50.687 回答