Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/212.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如果用户向左或向右按太多,则应用程序为FC_Java_Android_Eclipse_Android Layout - Fatal编程技术网

Java 如果用户向左或向右按太多,则应用程序为FC

Java 如果用户向左或向右按太多,则应用程序为FC,java,android,eclipse,android-layout,Java,Android,Eclipse,Android Layout,我有以下XML: <LinearLayout android:id="@+id/llColorSpect" android:layout_width="match_parent" android:layout_height="@dimen/color_scheme_height" android:orientation="vertical" android:background="@drawable/colorspect" android:l

我有以下XML:

<LinearLayout
    android:id="@+id/llColorSpect"
    android:layout_width="match_parent"
    android:layout_height="@dimen/color_scheme_height"
    android:orientation="vertical"
    android:background="@drawable/colorspect"
    android:layout_marginRight="@dimen/activity_horizontal_margin"
    android:layout_marginLeft="@dimen/activity_horizontal_margin"
    android:layout_marginBottom="@dimen/seek_bar_margin"
    android:layout_below="@+id/tvBGColor" >
    <RelativeLayout
        android:id="@+id/rlColorSpect"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
        <ImageView
            android:id="@+id/ivSquare"
            android:layout_width="@dimen/title_text_pad"
            android:layout_height="@dimen/title_text_pad"
            android:layout_alignParentBottom="true"
            android:scaleType="fitXY"
            android:src="@drawable/esquare" />
    </RelativeLayout>
</LinearLayout>
我遇到的问题

  • 当按下布局以获取布局某些部分的坐标时(如果我一直向左或向右按/拖动),我的应用程序会显示FC。我在下面添加日志:
  • 01-25 01:13:21.059:E/AndroidRuntime(13345):致命异常:主 01-25 01:13:21.059:E/AndroidRuntime(13345): java.lang.IllegalArgumentException:x必须 我想让用户能够一直拖动到左侧、顶部、右侧和底部。如何修改上述代码以实现这一点?谢谢

  • 在访问位图以查看返回的值之前,请将x和y日志记录移动到

  • 根据stacktrace,不要使用超过位图尺寸的值x和y调用getPixel

  • View.OnTouchListener llTouch = new View.OnTouchListener() {
    
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            int x = (int)event.getX();
            int y = (int)event.getY();
            int action = event.getAction();
            int pixel = bitmap.getPixel((int)x,(int) y);
    
            switch (action & MotionEvent.ACTION_MASK) {
                case MotionEvent.ACTION_DOWN: {
                    Log.i("COORDINATES","Touch coordinates : x" + String.valueOf(x) + "y" + String.valueOf(y));
                    ivTouch.setX(x);
                    ivTouch.setY(y);
                    int redValue = Color.red(pixel);
                    int blueValue = Color.blue(pixel);
                    int greenValue = Color.green(pixel);
                    Log.d("Colors","R:" +redValue +" G:" +greenValue+" B:" + blueValue);
                    sbRed.setProgress(redValue);
                    sbGreen.setProgress(greenValue);
                    sbBlue.setProgress(blueValue);
                    dispHVal();
                break;
                }
                case MotionEvent.ACTION_MOVE:{
                    Log.i("COORDINATES","Touch coordinates : x" + String.valueOf(x) + "y" + String.valueOf(y));
                    ivTouch.setX(x);
                    ivTouch.setY(y);
                    int redValue = Color.red(pixel);
                    int blueValue = Color.blue(pixel);
                    int greenValue = Color.green(pixel);
                    Log.d("Colors","R:" +redValue +" G:" +greenValue+" B:" + blueValue);
                    sbRed.setProgress(redValue);
                    sbGreen.setProgress(greenValue);
                    sbBlue.setProgress(blueValue);
                    dispHVal();
                break;
                }
            }
            return true;
        }
    };