Android - Creating a on click listener to a Button and a extended ImageView on the same activity
I have these things, in a activity i set a onTouchListener to my ImageDraw, that extends the ImageView class, with this listener I perform actions like zoom and pan with gestures
But in this activity i have a button to, but when a set the onClickListener to the button i get i NullPointerException.
Without setting the onClickListener everything works fine.
My ImageDraw class is:
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 o开发者_Go百科nDraw(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);
}
}
}
}
Edited:
Here is where i set the onClickListener to the button, and its here where te excpetion is throw. Exactly on the 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();
}
My best guess right now is that you don't set the content view in the activity. can you post your stack trace and activity code?
I've meet the similar question, but I found that I forget to setcontentView in the activity. Hope it can help you.
精彩评论