提前感谢您的帮助。我是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
答案 0 :(得分: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会很乐意将float转换为String。所以你的代码可以简单地是:
pixelcord.setText("X: " + eventX + "/ Y: " + eventY );
答案 1 :(得分:0)
你需要更多的“+”:
pixelcord.setText("X:" + String.valueOf(eventX) + "/ Y:" + String.valueOf(eventY) );
答案 2 :(得分:0)
pixelcord.setText("X:" String.valueOf(eventX) + "/ Y:" String.valueOf(eventY) );
应该是
pixelcord.setText("X:"+ String.valueOf(eventX) + "/ Y:"+ String.valueOf(eventY) );
答案 3 :(得分:0)
缺少+
用
替换行pixelcord.setText(“X:”+ String.valueOf(eventX)+“/ Y:”+ String.valueOf(eventY));
答案 4 :(得分:0)
setText()方法需要String
。因此,您只能使用字符串值,对于此问题,您必须使用+
符号
pixelcord.setText("X:"+ String.valueOf(eventX) + "/ Y:"+ String.valueOf(eventY) );