1

在此先感谢您的帮助。我是 Java 和 Android 编程的初学者,所以我的第一个应用程序只是在您拍照后尝试从照片中选择 rgb 值。但是,最后一个错误是“令牌“字符串”上的语法错误,删除此令牌”。这让我疯狂!似乎我所有的括号都在那里,每个语句的语法都是正确的,对发生的事情有什么想法吗?如果这有所不同,我正在使用 Eclipse,最新版本。这是完整的代码。问题从评论开始/* 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
4

5 回答 5

1

您缺少几个+(连接)运算符。而且您可能还想在“X:”之后放置一个空格以使其成为“X:”,以便很好地打印该值。

您的代码应该是:

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

而不是:

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

您也不需要该String.valueOf(...)函数,因为 Java 会很乐意为您将浮点数转换为字符串。所以你的代码可以简单地是:

pixelcord.setText("X: " + eventX + "/ Y: " + eventY );
于 2013-10-30T09:22:00.517 回答
0

你需要更多的“+”:

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

于 2013-10-30T09:22:15.983 回答
0
pixelcord.setText("X:" String.valueOf(eventX) + "/ Y:" String.valueOf(eventY) ); 

应该

pixelcord.setText("X:"+ String.valueOf(eventX) + "/ Y:"+ String.valueOf(eventY) ); 
于 2013-10-30T09:22:19.870 回答
0

失踪+

将行替换为

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

于 2013-10-30T09:23:41.723 回答
0

setText() 方法需要一个String. 所以你只使用字符串值,对于这个问题你必须与+符号连接

pixelcord.setText("X:"+ String.valueOf(eventX) + "/ Y:"+ String.valueOf(eventY) ); 
于 2013-10-30T09:23:53.007 回答