Android-在同一活动上创建Button的单击侦听器和扩展的ImageView
||
我有这些东西,在我将onTouchListener设置为ImageDraw的活动中,该活动扩展了ImageView类,并使用此侦听器执行手势缩放和平移之类的操作
但是在此活动中,我有一个按钮,但是将onClickListener设置为按钮时,我得到了NullPointerException。
如果不设置onClickListener,则一切正常。
我的ImageDraw类是:
public class ImageDraw extends ImageView{
private Paint mPaint = new Paint();
List<Point> pts = new ArrayList<Point>() ;
public ImageDraw(Context context) {
super(context);
}
//used to send the location of the points to draw on the screen
//must be called before every redraw to update the points on the screen
public void SetPointsToDraw(List<Point> pts)
{
this.pts = pts;
}
public ImageDraw(Context context, AttributeSet attrs)
{
super(context,attrs);
}
public ImageDraw(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
}
@Override
public void onDraw(Canvas canvas)
{
super.onDraw(canvas);
Paint paintColor = mPaint;
paintColor.setColor(Color.YELLOW);
paintColor.setStrokeWidth(3);
if(pts.size() > 0)
{
canvas.drawCircle(pts.get(0).x, pts.get(0).y, 7, paintColor);
}
if (pts.size() > 1)
{
for (int i = 1 ; i < pts.size(); i++) {
paintColor.setColor(Color.YELLOW);
canvas.drawCircle(pts.get(i).x, pts.get(i).y, 7, paintColor);
paintColor.setColor(Color.RED);
canvas.drawLine(pts.get(i-1).x, pts.get(i-1).y, pts.get(i).x, pts.get(i).y, paintColor);
}
}
}
}
编辑:
这是我将onClickListener设置为按钮的地方,这里是te引起异常的地方。完全在btnNew.SetOnTouchListener上
Button btnNew = (Button) findViewById(R.id.btnNew);
try
{
btnNew.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
Intent intent = new Intent(getApplicationContext(), NewWaypoint.class);
startActivity(intent);
return false;
}
});
}
catch(Exception e)
{
String teste = e.toString();
}
没有找到相关结果
已邀请:
2 个回复
草票
穗揣