【问题标题】:java.lang.IllegalArgumentException: y must be < bitmap.height()java.lang.IllegalArgumentException: y 必须是 < bitmap.height()
【发布时间】:2013-03-30 12:26:42
【问题描述】:

我有这种方法可以与 xhdpi 中的图像进行比较,但给了我这个错误 java.lang.IllegalArgumentException: y must be 这里是代码:

 private boolean CompareImages(ImageView im1, ImageView im2){

            boolean bool = false;
            int check = 0, v1, v2, v3, ev1, ev2, ev3;
            Bitmap bitmap1 = ((BitmapDrawable)im1.getDrawable()).getBitmap();
            Bitmap bitmap2 = ((BitmapDrawable)im2.getDrawable()).getBitmap();


            for(int j = 50; j < 160; j = j + 50){
                int pixel = bitmap1.getPixel(j,j);
                int pixel2 = bitmap2.getPixel(j,j);

                int redValue = Color.red(pixel);
                int blueValue = Color.blue(pixel);
                int greenValue = Color.green(pixel);

                v1 = (redValue + greenValue + blueValue)/3;

                int redValue1 = Color.red(pixel2);
                int blueValue2 = Color.blue(pixel2);
                int greenValue2 = Color.green(pixel2);

                ev1 = (redValue1 + greenValue2 + blueValue2)/3;

                if(v1 == ev1){
                    bool = true;
                } else{
                    bool = false;
                }
            }

            return bool;
}

这里是logcat:

03-31 10:53:45.192: E/AndroidRuntime(807): FATAL EXCEPTION: main
03-31 11:29:33.352: E/AndroidRuntime(1574): FATAL EXCEPTION: main
03-31 11:29:33.352: E/AndroidRuntime(1574): java.lang.IllegalStateException: Could not execute method of the activity
03-31 11:29:33.352: E/AndroidRuntime(1574):     at android.view.View$1.onClick(View.java:3599)
03-31 11:29:33.352: E/AndroidRuntime(1574):     at android.view.View.performClick(View.java:4204)
03-31 11:29:33.352: E/AndroidRuntime(1574):     at android.view.View$PerformClick.run(View.java:17355)
03-31 11:29:33.352: E/AndroidRuntime(1574):     at android.os.Handler.handleCallback(Handler.java:725)
03-31 11:29:33.352: E/AndroidRuntime(1574):     at android.os.Handler.dispatchMessage(Handler.java:92)
03-31 11:29:33.352: E/AndroidRuntime(1574):     at android.os.Looper.loop(Looper.java:137)
03-31 11:29:33.352: E/AndroidRuntime(1574):     at android.app.ActivityThread.main(ActivityThread.java:5041)
03-31 11:29:33.352: E/AndroidRuntime(1574):     at java.lang.reflect.Method.invokeNative(Native Method)
03-31 11:29:33.352: E/AndroidRuntime(1574):     at java.lang.reflect.Method.invoke(Method.java:511)
03-31 11:29:33.352: E/AndroidRuntime(1574):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-31 11:29:33.352: E/AndroidRuntime(1574):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-31 11:29:33.352: E/AndroidRuntime(1574):     at dalvik.system.NativeStart.main(Native Method)
03-31 11:29:33.352: E/AndroidRuntime(1574): Caused by: java.lang.reflect.InvocationTargetException
03-31 11:29:33.352: E/AndroidRuntime(1574):     at java.lang.reflect.Method.invokeNative(Native Method)
03-31 11:29:33.352: E/AndroidRuntime(1574):     at java.lang.reflect.Method.invoke(Method.java:511)
03-31 11:29:33.352: E/AndroidRuntime(1574):     at android.view.View$1.onClick(View.java:3594)
03-31 11:29:33.352: E/AndroidRuntime(1574):     ... 11 more
03-31 11:29:33.352: E/AndroidRuntime(1574): Caused by: java.lang.IllegalArgumentException: y must be < bitmap.height()
03-31 11:29:33.352: E/AndroidRuntime(1574):     at android.graphics.Bitmap.checkPixelAccess(Bitmap.java:1158)
03-31 11:29:33.352: E/AndroidRuntime(1574):     at android.graphics.Bitmap.getPixel(Bitmap.java:1107)
03-31 11:29:33.352: E/AndroidRuntime(1574):     at com.example.gusetheplace.ConductQuiz2.CompareImages(ConductQuiz2.java:794)
03-31 11:29:33.352: E/AndroidRuntime(1574):     at com.example.gusetheplace.ConductQuiz2.onClick_rb4(ConductQuiz2.java:630)
03-31 11:29:33.352: E/AndroidRuntime(1574):     ... 14 more

【问题讨论】:

  • 请您发布完整的 logcat。

标签: android android-layout android-image


【解决方案1】:

您正在迭代int j = 50; j &lt; 160; j = j + 50,而不检查图像的实际大小。

您应该使用Bitmap.getHeight()Bitmap.getWidth() 以便使用有效坐标调用Bitmap.getPixel(x,y)

【讨论】:

  • 是的。图像像素小于循环中的索引
猜你喜欢
  • 1970-01-01
  • 2016-05-07
  • 1970-01-01
  • 1970-01-01
  • 2021-05-29
  • 1970-01-01
  • 2017-05-13
  • 2018-03-31
  • 2022-10-11
相关资源
最近更新 更多