Search code examples
androidimageviewtap

Very strange syntax error in code source from ADT


I have an activity that is working well. There is an imageview. I want to get the user tap on this image. Application run on emulator and also I am testing it on my kindle-hd. No problem. I found many examples about Android Imageview setOnTouchListener. So, I get one of these and I have put it into my code, at the bottom of class. I checked it many time to find errors but I have not found. I am new on Android. So I believe that I am not able to understand where is the error. If I remove the lines of code from //CODE START HERE and //CODE END HERE, all works. Any idea? Many thanks. Sergio

} End of previous function I get **Syntax error on token "}", delete this token**

    //CODE START HERE
ImageView imgHalfCourt =(ImageView) findViewById(R.id.imgHalfCourt);
imgHalfCourt.setOnTouchListener(new View.OnTouchListener(){
    @Override
    public boolean onTouch(View v, MotionEvent event)
    {
        ImageView imgHalfCourt =(ImageView) findViewById(R.id.imgHalfCourt);
        final Bitmap bitmap =((BitmapDrawable)imgHalfCourt.getDrawable()).getBitmap();
        int x = (int)event.getX();
        int y = (int)event.getY();
        int pixel = bitmap.getPixel(x,y);

        //then do what you want with the pixel data, e.g
        int redValue = Color.red(pixel);
        int blueValue = Color.blue(pixel);
        int greenValue = Color.green(pixel);  
        return true;
    }
});
    //CODE END HERE

} End of Class I get Syntax error, insert "}" to complete ClassBody


Solution

  • Strange syntax error? Not at all, I guess!

    Just cut your code between //CODE START HERE and //CODE END HERE and copy it to your Activitie's onCreate().