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();
    }
    
已邀请:
目前,我最好的猜测是您没有在活动中设置内容视图。您可以发布堆栈跟踪和活动代码吗?     
我遇到了类似的问题,但是我发现我忘记了在活动中使用setcontentView了。希望它能对您有所帮助。     

要回复问题请先登录注册