我有這些東西,在一個活動中,我爲我的ImageDraw設置了一個onTouchListener,它擴展了ImageView類,縮放和平移與手勢Android - 在同一個活動上創建一個按鈕和一個擴展的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();
}
你可以把代碼給出一個錯誤 - ' – JQCorreia 2011-04-14 19:09:23
如果它在佈局中,看起來像你的佈局可能不會膨脹。需要更多的信息,你可以在button上設置onClickListener – chaitanya 2011-04-14 19:19:36
你知道從哪裏拋出NullPointerException嗎? – shihpeng 2011-04-14 19:21:13