Android Studio应用在按钮点击时崩溃

时间:2016-01-22 15:21:34

标签: java android android-canvas

最近我修复了一个错误,但是现在我一直在为我的绘图项目清除画布。我已经用同样的问题看了一些其他主题,但它没有解决我的问题。

package org.arkmap.klaas.testdrawing;

import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;

public class WriteOnScreenActivity extends AppCompatActivity {
    Button clear;
    TouchEventView myView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_write_on_screen);
        clear=(Button)findViewById(R.id.clearbutton);
        myView = new TouchEventView(this, null);
        clear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View vw) {
                myView.clearCanvas();
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_write_on_screen, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

WriteOnScreenActivity.java

package org.arkmap.klaas.testdrawing;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import java.util.Random;
/**
 * Created by Startklaar on 19-1-2016.
 */
public class TouchEventView extends View {
    public Paint paint = new Paint();
    private Path path = new Path();
    public boolean cc = false;

    Random rnd = new Random();

    public TouchEventView(Context ctx, AttributeSet attrs) {
        super(ctx, attrs);
        paint.setAntiAlias(true);
        paint.setARGB(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));
        //paint.setColor(Color.WHITE);
        paint.setStrokeJoin(Paint.Join.ROUND);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(10f);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        if (cc) {
            path = new Path();
            Paint clearPaint = new Paint();
            clearPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
            canvas.drawRect(0, 0, 0, 0, clearPaint);
            cc = false;
        }
        canvas.drawPath(path, paint);
    }

    public void clearCanvas()
    {
        cc = true;
        invalidate();
    }

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

        switch(event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                path.moveTo(xPos, yPos);
                return true;
            case MotionEvent.ACTION_MOVE:
                path.lineTo(xPos,yPos);
                break;
            case MotionEvent.ACTION_UP:
                break;
            default:
                return false;
        }

        invalidate();
        return true;
    }
}

TouchEventView.java

希望能在这里得到一些帮助!

编辑:错误已修复,我唯一的问题是该按钮无法正常工作。

1 个答案:

答案 0 :(得分:1)

myView未初始化。你应该设置它:

 myView = (TouchEventView) findViewById(R.id.XXXX);