【问题标题】:Getting exception in assignment of a value to array in android在将值分配给android中的数组时出现异常
【发布时间】:2013-10-17 18:49:54
【问题描述】:

我正在尝试逐像素读取图像,并希望将像素 RGB 值保存在数组中。 但是当我尝试这样做时,我在声明中遇到了一个异常
R[i]=redValue;
有人请建议我可能做错了什么?

请在此处找到异常堆栈跟踪 -

paste.ubuntu.com/6216208

public class AnalyzeImage extends Activity  
{   
    int []R;  
    int []G;  
    int []B;  
    int width, height, i=0;  
    int a=1;  
    public int analyzeImagefunc(Bitmap bitmap)
    {            
    try{ 
     width  =  bitmap.getWidth();
     height = bitmap.getHeight();
     i = 0;
     for(int x = 0;x < width;x++)
     {
      for(int y = 0;y < height;y++)
      {
      int pixel = bitmap.getPixel(x,y);         
      int redValue = Color.red(pixel);
      int blueValue = Color.blue(pixel);
      int greenValue = Color.green(pixel);
      a=5;
      R[i]=redValue; 
      a=6;
      i++;
       }
      } 
     }

    catch(Exception e)
        {
            //nothing
        }
     return a;
 }  
} 

【问题讨论】:

  • 数组未初始化

标签: java android variable-assignment


【解决方案1】:

int []R;
整数 []G;
整数 []B;

R、G、B 未在任何地方初始化。

初始化R

 width  =  bitmap.getWidth();
 height = bitmap.getHeight();
 i = 0;
 int size = height * width;
 R = new int[size]; // initialize the size of array R   
 for(int x = 0;x < width;x++)

【讨论】:

    【解决方案2】:

    您的数组在使用前需要先初始化。你已经声明了数组:

    int []R;  
    int []G;  
    int []B;  
    

    但没有初始化它们。因此,如果您尝试访问任何数组元素,它将抛出空指针异常。尝试像这样初始化你的数组:

    int []R = new int[10];  
    int []G = new int[10];  
    int []B = new int[10];  
    

    您可以根据需要更改数组的大小。

    【讨论】:

      【解决方案3】:

      数组需要以固定大小初始化。所以如果数组的大小不固定,那么你应该使用 ArrayList。如果最终输出需要,您还可以将 ArrayList 转换为数组。

          public class AnalyzeImage extends Activity {
              int[] R;
              int[] G;
              int[] B;
              ArrayList<Integer> R_Lst = new ArrayList<Integer>();
              ArrayList<Integer> G_Lst = new ArrayList<Integer>();
              ArrayList<Integer> B_Lst = new ArrayList<Integer>();
      
              int width, height, i = 0;
              int a = 1;
      
              public int analyzeImagefunc(Bitmap bitmap) {
                  try {
                      width = bitmap.getWidth();
                      height = bitmap.getHeight();
                      i = 0;
                      for (int x = 0; x < width; x++) {
                          for (int y = 0; y < height; y++) {
                              int pixel = bitmap.getPixel(x, y);
                              int redValue = Color.red(pixel);
                              int blueValue = Color.blue(pixel);
                              int greenValue = Color.green(pixel);
                              a = 5;
      //                      R[i] = redValue;
                              R_Lst.add(redValue);
                              B_Lst.add(blueValue);
                              G_Lst.add(greenValue);
                              a = 6;
                              i++;
                          }
                      }
                  }
      
                  catch (Exception e) {
                      // nothing
                  }
                  R=convertIntegers(R_Lst);
                  return a;
              }
              public int[] convertIntegers(List<Integer> integers)
              {
                  int[] ret = new int[integers.size()];
                  for (int i=0; i < ret.length; i++)
                  {
                      ret[i] = integers.get(i).intValue();
                  }
                  return ret;
              }
      
      //      (Note that this will throw a NullPointerException if either integers or any element within it is null.)
      

      【讨论】:

      • 想要将像素 RGB 值保存在数组中。他想将 RGB 值存储在 Array 中
      【解决方案4】:
      width = bitmap.getWidth();
      height = bitmap.getHeight();
      int n = width * height;
      R = new int[n];  
      G = new int[n];  
      B = new int[n];
      i = 0;
      

      暗示,您还需要:

      R[i] = redValue;
      G[i] = greenValue;
      B[i] = blueValue;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-05-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-06-13
        • 2021-09-20
        • 2012-01-27
        相关资源
        最近更新 更多