1

thanks in advance for the help. I'm a beginner at Java and Android programming so my first app is just trying to pick rgb values from a photo after you take a picture. However, the last error is "Syntax Error On Token "String", delete this token". It's driving me crazy! It seems all my brackets are there, the syntax for each statement is correct, any ideas to what is going on? I'm using Eclipse if that makes a difference, the latest build. Here's the full code. The issue begins at the comment /* SET TEXT FUNCTION TO THE FIELD USING SET TEXT METHOD*/.

package com.example.firstapp;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends Activity { //1

    TextView pixelcord, rgbvals; 
    ImageView iv;
    Button btn;



    @Override
    public void onCreate(Bundle savedInstanceState) { //2

        /* GATHER THE INFORMATION FROM THE LAYOUT TO ORGANIZE APP*/
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        /* SET ON CLICK LISTENER TO GET CLICK REPSONSE -- LAUNCH CAMERA AND TAKE PHOTO */
        /* SET VARIABLES FOR USE FROM EACH VIEW */
        iv = (ImageView) findViewById(R.id.imageView); /* USE IMAGE VIEW FIELD*/
        pixelcord = (TextView)findViewById(R.id.pixelcord); /* USE TEXT FIELD FOR PIXEL */
        rgbvals = (TextView)findViewById(R.id.rgbvals); /* USE TEXT FIELD FOR RGB VALUES*/
        btn = (Button) findViewById(R.id.takePhoto);

        /* SET INFORMATION OF WHAT TO DO UPON EACH CLIK*/
        iv.setOnTouchListener(imgSourceOnTouchListener);

/* =====================================CAMERA BUTTON=====================================*/        
        btn.setOnClickListener(new OnClickListener() { //3

            @Override

            public void onClick(View v) { //4

                Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                startActivityForResult(intent, 0);

            } //*4


        }); //*3
/* =======================================================================================*/        


        } /* END OF ON CREATE*/ //*2

    /* DECLARATION OF IMG TOUCH FUNCTION*/

    OnTouchListener imgSourceOnTouchListener = new OnTouchListener() { //5

        @Override

        public boolean onTouch(View view, MotionEvent event) { //6


            float eventX = event.getX();
            float eventY = event.getY();
            float[] eventXY = new float[] { eventX, eventY};

            Matrix invertMatrix = new Matrix(); 
            ((ImageView)view).getImageMatrix().invert(invertMatrix);

            invertMatrix.mapPoints(eventXY); 
            int x = Integer.valueOf((int)eventXY[0]); /* POTENTIALLY REDUNDANT*/
            int y = Integer.valueOf((int)eventXY[1]);


            /* SET TEXT FUNCTION TO THE FIELD USING SET TEXT METHOD*/
            pixelcord.setText("X:" String.valueOf(eventX) + "/ Y:" String.valueOf(eventY) ); 


            int touchedRGB = iv.getDrawingCache().getPixel(x,y);

            rgbvals.setText("Color Value" + "#" + Integer.toHexString(touchedRGB));
            rgbvals.setTextColor(touchedRGB);

            return true;

        } //*6


    }; //*5

    @Override

    protected void onActivityResult( int requestCode, int resultCode, Intent data)
    { //7
        if(requestCode == 0)
        { //8
            Bitmap theImage = (Bitmap) data.getExtras().get("data");
            iv.setImageBitmap(theImage);

        } //*8
    } //*7


} //*1
1
  • Thank you all! That solved my problem, but now I have one more question. The photo button works and sends the picture to the view, but when I touch the image the program crashes as opposed to giving me the RGB value of the location touched. Any ideas why? Commented Oct 30, 2013 at 9:38

5 Answers 5

1

You are missing a couple of + (concatination) operators. And you might also want to put a space after "X:" to make it "X: " so that the value prints nicely.

Your code should be:

pixelcord.setText("X: " + String.valueOf(eventX) + "/ Y: " + String.valueOf(eventY) ); 

Rather than:

pixelcord.setText("X:" String.valueOf(eventX) + "/ Y:" String.valueOf(eventY) ); 

Also you don't need the String.valueOf(...) function because Java will happily convert the float to a String for you. So your code can simply be:

pixelcord.setText("X: " + eventX + "/ Y: " + eventY );
Sign up to request clarification or add additional context in comments.

15 Comments

That solved my problem, but now I have one more question. The photo button works and sends the picture to the view, but when I touch the image the program crashes as opposed to giving me the RGB value of the location touched. Any ideas why?
Running it on my phone it just says the application has stopped. I'm assuming it has to do with the imgSourceonTouchListener method
I will need to see an error to really help you. The only thing I noticed when having a quick look was: int x = Integer.valueOf((int)eventXY[0]); could just be: int x = (int)eventXY[0]; although you need to be aware that numbers such as 5.453 would become 5 (the decimals are chopped off). That being said, this won't cause an error its just something I noticed.
Ok, I'm installing LogViewer, I should be able to tell you in a little bit
Ok, there is a runtime exception "Failure delivering result ResultINfo(who=null, request = 0, result = 0, data = null) to activity MainActivity. There's a runtime error at line 91 (the line that is supposed to give the touchedRGB variable the values. Does this help?
|
0

You need some more "+":

pixelcord.setText("X:" + String.valueOf(eventX) + "/ Y:" + String.valueOf(eventY) );

Comments

0
pixelcord.setText("X:" String.valueOf(eventX) + "/ Y:" String.valueOf(eventY) ); 

should be

pixelcord.setText("X:"+ String.valueOf(eventX) + "/ Y:"+ String.valueOf(eventY) ); 

Comments

0

Missing +

Replace line with

pixelcord.setText("X:" + String.valueOf(eventX) + "/ Y:" + String.valueOf(eventY) );

Comments

0

The setText() method takes a String. So you to use only string values, for this problem you have to concatenate with the + symbol

pixelcord.setText("X:"+ String.valueOf(eventX) + "/ Y:"+ String.valueOf(eventY) ); 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.