1
 public void Pattern(Canvas canvas) {

            int x = 0;
            int y = 0;
            Paint paint = new Paint();
            paint.setStyle(Paint.Style.FILL);

            paint.setColor(Color.WHITE);
            canvas.drawPaint(paint);

            paint.setStyle(Paint.Style.STROKE);
            paint.setStrokeWidth(1);
            paint.setAntiAlias(true);
            paint.setColor(Color.MAGENTA);
            paint.setTextSize(22);
            canvas.drawText("movement pattern", 75, 55, paint);


            paint.setStyle(Paint.Style.FILL);



            paint.setColor(Color.RED);
            canvas.drawCircle(359, 300 , 4, paint);
            canvas.drawCircle(425, 300, 4, paint);
        //  canvas.drawCircle(248, , 4, paint);
            canvas.drawCircle(248, 380, 4, paint);
            canvas.drawCircle(50, 300, 4, paint);
            //canvas.drawCircle(5421, 6499, 4, paint);
            // draw a thick dashed line,
            paint.setColor(Color.BLUE);
            canvas.drawLine(359, 300 , 425, 300,  paint);
            canvas.drawLine(425, 300, 248, 380,  paint);
            //canvas.drawLine(400, 500 ,248, 380,  paint);
            canvas.drawLine(248, 380 , 50, 300,  paint);
            canvas.drawLine(50, 300, 359, 300,  paint);

        }

活动主.xml:

<Button
    android:id="@+id/BPV"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/View"
    android:layout_below="@+id/btnShowLocation"
    android:layout_marginLeft="15dp"
    android:onClick="Pattern"
    android:text="Pattern" />

<View
    android:id="@+id/PV"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/BPV"
    android:layout_below="@+id/TextView1" />

基本上,在主布局中,我有 4 个按钮并且一切正常。但除了BPV 按钮。单击BPV 按钮时,我想在PV处显示Pattern(Canvas canvas)。但是单击它时应用程序会强制关闭。

4

1 回答 1

0

方法的方法签名Pattern不正确。在 XML 中用作 android:onClick 时,它必须public void Pattern(View view)

您需要找出另一种方法来尝试执行操作,当您单击按钮时,您无权访问视图的画布。

于 2013-07-17T12:11:16.433 回答