0

我正在尝试从文本字段中获取 RGB 值(不是十六进制,实际的常量值,例如 -16711936)并将其显示在新活动中(将其用作整数)。我试图通过将字符串转换为整数来做到这一点,但它仍然最终导致程序崩溃。这是代码:

完整来源: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

这只是一个语法错误还是我做错了?我很感激帮助

注意:文本字段中的值在技术上是我猜的字符串,这是我用来将它放在字段中的代码(touchedRGB 是一个整数值)。

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" />
4

3 回答 3

2

问题是,您的 textView 中有一个字符串(“RGB”)。所以你得到了一个 NumberFormatException。在您的 textView 中设置一个整数值,然后重试。

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像我上面写的那样改变你的方法。你的问题将得到解决。

于 2013-10-31T11:54:22.440 回答
0
Integer rgb = Integer.getInteger((String) RGBval.getText(), null);
if (rgb == null) {
    // input string is not integer
} else {
    // use rgb to start new activity
}
于 2013-10-31T11:52:21.007 回答
0

试试这个,你不能像这样将 int 值转换为 String -> (int)(rgb_val_use)

将其更改为 new Integer.toString(rgb_val_use);

intent.putExtra("RGB_VALUE",new Integer.toString(rgb_val_use));
于 2013-10-31T11:42:28.110 回答