我正在尝试从文本字段中获取RGB值(不是十六进制,实际常量值,例如-16711936)并将其显示在新活动中(将其用作整数)。我试图通过将字符串转换为整数来完成此操作,但它仍然会导致程序崩溃。这是代码:
FULL SOURCE:问题在testvalue函数中出现
package com.example.seniordesign;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
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
//public final static String RGB_VALUE = "com.example.seniordesign.RGB_VALUE_USE";
TextView pixelcord, rgbvals, rgbnumvals, red, green, blue, RGB;
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);
red = (TextView)findViewById(R.id.red);
blue = (TextView)findViewById(R.id.blue);
green = (TextView)findViewById(R.id.green);
RGB = (TextView)findViewById(R.id.RGB);
/* =====================================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]);
/* CHECK TO MAKE SURE VALUES ARE WITHIN BITMAP RANGE*/
/* SET TEXT FUNCTION TO THE FIELD USING SET TEXT METHOD*/
pixelcord.setText("X:" + String.valueOf(eventX) + "/ Y:" + String.valueOf(eventY) );
Bitmap bitmap = ((BitmapDrawable)iv.getDrawable()).getBitmap();
int touchedRGB = bitmap.getPixel(x,y);
rgbvals.setText("Color Value" + "#" + Integer.toHexString(touchedRGB));
rgbvals.setTextColor(touchedRGB);
int rval = Color.red(touchedRGB);
int bval = Color.blue(touchedRGB);
int gval = Color.green(touchedRGB);
red.setText(String.valueOf(rval));
blue.setText(String.valueOf(bval));
green.setText(String.valueOf(gval));
RGB.setText(String.valueOf(touchedRGB));
return true;
} //*6
}; //*5
public void testvalue(View view) {
Intent intent = new Intent(this, TestValue.class);
TextView RGBval = (TextView) findViewById(R.id.RGB);
int rgb_val_use = Integer.parseInt((String) RGBval.getText().toString());
intent.putExtra("RGB_VALUE", (int)rgb_val_use);
startActivity(intent);
}
@Override
public 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
这是我的logCat:
10-31 07:27:44.334: E/AndroidRuntime(9915): FATAL EXCEPTION: main
10-31 07:27:44.334: E/AndroidRuntime(9915): java.lang.IllegalStateException: Could not execute method of the activity
10-31 07:27:44.334: E/AndroidRuntime(9915): at android.view.View$1.onClick(View.java:2144)
10-31 07:27:44.334: E/AndroidRuntime(9915): at android.view.View.performClick(View.java:2485)
10-31 07:27:44.334: E/AndroidRuntime(9915): at android.view.View$PerformClick.run(View.java:9089)
10-31 07:27:44.334: E/AndroidRuntime(9915): at android.os.Handler.handleCallback(Handler.java:587)
10-31 07:27:44.334: E/AndroidRuntime(9915): at android.os.Handler.dispatchMessage(Handler.java:92)
10-31 07:27:44.334: E/AndroidRuntime(9915): at android.os.Looper.loop(Looper.java:123)
10-31 07:27:44.334: E/AndroidRuntime(9915): at android.app.ActivityThread.main(ActivityThread.java:3806)
10-31 07:27:44.334: E/AndroidRuntime(9915): at java.lang.reflect.Method.invokeNative(Native Method)
10-31 07:27:44.334: E/AndroidRuntime(9915): at java.lang.reflect.Method.invoke(Method.java:507)
10-31 07:27:44.334: E/AndroidRuntime(9915): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
10-31 07:27:44.334: E/AndroidRuntime(9915): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
10-31 07:27:44.334: E/AndroidRuntime(9915): at dalvik.system.NativeStart.main(Native Method)
10-31 07:27:44.334: E/AndroidRuntime(9915): Caused by: java.lang.reflect.InvocationTargetException
10-31 07:27:44.334: E/AndroidRuntime(9915): at java.lang.reflect.Method.invokeNative(Native Method)
10-31 07:27:44.334: E/AndroidRuntime(9915): at java.lang.reflect.Method.invoke(Method.java:507)
10-31 07:27:44.334: E/AndroidRuntime(9915): at android.view.View$1.onClick(View.java:2139)
10-31 07:27:44.334: E/AndroidRuntime(9915): ... 11 more
10-31 07:27:44.334: E/AndroidRuntime(9915): Caused by: java.lang.NumberFormatException: unable to parse 'RGB' as integer
10-31 07:27:44.334: E/AndroidRuntime(9915): at java.lang.Integer.parse(Integer.java:383)
10-31 07:27:44.334: E/AndroidRuntime(9915): at java.lang.Integer.parseInt(Integer.java:372)
10-31 07:27:44.334: E/AndroidRuntime(9915): at java.lang.Integer.parseInt(Integer.java:332)
10-31 07:27:44.334: E/AndroidRuntime(9915): at com.example.seniordesign.MainActivity.testvalue(MainActivity.java:125)
10-31 07:27:44.334: E/AndroidRuntime(9915): ... 14 more
这只是一个语法错误还是我错误地解决了这个问题?我很感激帮助
注意:textfield中的值在技术上是我猜的字符串,这是我用来将它放在字段中的代码(touchRGB是一个整数值)。
RGB.setText(String.valueOf(touchedRGB));
如果它有所不同,那就是这个特定部分的XML
<TextView
android:id="@+id/RGB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/green"
android:layout_alignBottom="@+id/green"
android:layout_marginLeft="40dp"
android:layout_toRightOf="@+id/green"
android:text="RGB" />
答案 0 :(得分:2)
问题是,textView中有一个字符串(“RGB”)。所以你得到了一个NumberFormatException。 在textView中设置integet值,然后重试。
java.lang.NumberFormatException: unable to parse 'RGB' as integer
编辑:您可以尝试以下代码吗?
public void testvalue(View view) {
try{
TextView RGBval = (TextView) findViewById(R.id.RGB);
String valueStoredInRGBvalTextView = RGBval.getText().toString();
int rgb_val_use = Integer.parseInt(valueStoredInRGBvalTextView);
Intent intent = new Intent(this, TestValue.class);
intent.putExtra("RGB_VALUE", (int)rgb_val_use);
startActivity(intent);
} catch (Exception e) {
// Not touched yet
}
}
EDIT2:
首次触摸后,您将整数触摸值设置为RGB textView。如果您在触摸任何地方之前调用testvalue
方法,则会遇到此异常。如上所述,更改您的testvalue
方法。你的问题将得到解决。
答案 1 :(得分:0)
试试这个你不能像这样将int值转换为String - &gt; (INT)(rgb_val_use)
将其更改为new Integer.toString(rgb_val_use)
;
intent.putExtra("RGB_VALUE",new Integer.toString(rgb_val_use));
答案 2 :(得分:0)
Integer rgb = Integer.getInteger((String) RGBval.getText(), null);
if (rgb == null) {
// input string is not integer
} else {
// use rgb to start new activity
}