最近我修復了一個錯誤,但現在我被困在清理畫布的繪製項目。我已經看過一些其他相同問題的主題,但它沒有解決我的問題。Android Studio應用程序崩潰按鈕單擊
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
希望在這裏得到一些幫助!
編輯:錯誤已修復,我唯一的問題是,該按鈕不起作用。
你的錯誤是什麼? – Ferrybig
「myView」尚未初始化 –
Logs/logcat是你最好的朋友〜利用它! – Jiyeh