Android - 在同一个活动上创建一个按钮和一个扩展的ImageView的点击监听器

问题描述:

我有这些东西,在一个活动中,我为我的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(); 
    } 
+0

你可以把代码给出一个错误 - ' – JQCorreia 2011-04-14 19:09:23

+0

如果它在布局中,看起来像你的布局可能不会膨胀。需要更多的信息,你可以在button上设置onClickListener – chaitanya 2011-04-14 19:19:36

+0

你知道从哪里抛出NullPointerException吗? – shihpeng 2011-04-14 19:21:13

我最好的猜测现在的问题是,你没有设置在活动内容视图。你可以发布你的堆栈跟踪和活动代码吗?

我遇到了类似的问题,但我发现我忘记了在活动中设置内容视图。希望它能帮助你。