我目前正在关注此answer。从这个答案可以看出,如果触摸了像素/网格,它将被检查。但是,我想检查一个网格作为起点,并在主要活动中设置控制/按钮以移动起点。例如,如果用户单击“前进”按钮,则起点将向前移动,检查新网格并将前一点设置为白色。现在我在moveForward()方法中的checkedCell [column] [row]上得到nullpointerexception
所以,这是我的代码:
public class PixelGridView extends View {
private int numColumns, numRows;
private int cellWidth, cellHeight;
private Paint blackPaint = new Paint();
private boolean[][] cellChecked;
public PixelGridView(Context context)
{
this(context, null);
}
public PixelGridView(Context context, AttributeSet attrs)
{
super(context, attrs);
blackPaint.setStyle(Paint.Style.FILL_AND_STROKE);
}
public void setNumColumns(int numColumns)
{
this.numColumns = numColumns;
calculateDimensions();
}
public int getNumColumns()
{
return numColumns;
}
public void setNumRows(int numRows)
{
this.numRows = numRows;
calculateDimensions();
}
public int getNumRows()
{
return numRows;
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh)
{
super.onSizeChanged(w, h, oldw, oldh);
calculateDimensions();
}
private void calculateDimensions()
{
if (numColumns == 0 || numRows == 0)
return;
cellWidth = getWidth() / numColumns;
cellHeight = getHeight() / numRows;
cellChecked = new boolean[numColumns][numRows];
invalidate();
}
@Override
protected void onDraw(Canvas canvas)
{
super.onDraw(canvas);
canvas.drawColor(Color.WHITE);
if (numColumns == 0 || numRows == 0)
return;
int width = getWidth();
int height = getHeight();
for (int i = 0; i < numColumns; i++)
{
for (int j = 0; j < numRows; j++)
{
if (cellChecked[i][j])
{
canvas.drawRect(i * cellWidth, j * cellHeight, (i + 1) * cellWidth, (j + 1) * cellHeight, blackPaint);
}
}
}
for (int i = 1; i < numColumns; i++)
{
canvas.drawLine(i * cellWidth, 0, i * cellWidth, height, blackPaint);
}
for (int i = 1; i < numRows; i++)
{
canvas.drawLine(0, i * cellHeight, width, i * cellHeight, blackPaint);
}
}
@Override
public boolean onTouchEvent(MotionEvent event)
{
if (event.getAction() != MotionEvent.ACTION_DOWN)
return true;
int column = (int)(event.getX() / cellWidth);
int row = (int)(event.getY() / cellHeight);
cellChecked[column][row] = !cellChecked[column][row];
invalidate();
return true;
}
public void moveForward() {
//set column and row to be checked at [1][1] upon button click on mainactivity
int column = 1;
int row = 1;
cellChecked[column][row] = !cellChecked[column][row];
invalidate();
}
}
这是我调用moveForward()方法的活动
public class MainActivity extends Activity {
PixelGridView pgv;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pgv = new PixelGridView(this);
pgv.setNumColumns(20);
pgv.setNumRows(15);
}
public void onBtnForwardPressed(View view) {
pgv.moveForward();
}
}
以及日志:
05-10 00:36:33.388: E/AndroidRuntime(8414): Caused by: java.lang.NullPointerException
05-10 00:36:33.388: E/AndroidRuntime(8414): at com.test.PixelGridView.moveForward(PixelGridView.java:131)
05-10 00:36:33.388: E/AndroidRuntime(8414): at com.test.MainActivity.onBtnForwardPressed(MainActivity.java:281)
moveForward()方法移动机器人:
if (currentAngle == 0) {
for (int i = 0; i < numColumns; i++) {
for (int j = 0; j < numRows; j++) {
if (cellChecked[i][j]) {
column1 = i;
row1 = j;
}
}
}
cellChecked[column1][row1] = !cellChecked[column1][row1];
cellChecked[column1+1][row1] = !cellChecked[column1+1][row1];
invalidate();
}
答案 0 :(得分:1)
您使用PixelGridView
创建的new
对象不在活动的布局中,因为您尚未添加它。如果您已在PixelGridView
布局中定义activity_main
,则应使用pgv
方法将其分配给findViewById()
。如果您打算以编程方式创建它,那么您应该使用addContentView()
方法将其添加到Activity的布局中。