2016-02-01 158 views
0

我從插座獲取圖像:setBitmap拋出空指針異常

 mByteArray = new char[size+1]; 
     int count = 0; 
     r.read(mByteArray,0, size); 
     int nbrOfPixels = width*height; 
     Log.i("nbrOfPixels", String.valueOf(nbrOfPixels)); 

     int pixels[] = new int[nbrOfPixels]; 
     for(int i = 0; i < nbrOfPixels; i++) { 
      int r = mByteArray[3*i]; 
      int g = mByteArray[3*i + 1]; 
      int b = mByteArray[3*i + 2]; 
      pixels[i] = Color.rgb(r, g, b); 
     } 
     bitmap = Bitmap.createBitmap(pixels, width, height, Bitmap.Config.ARGB_8888); 

在OnCreate中的ImageView被定義爲:

imageView = (ImageView)findViewById(R.id.imgView); 

和異步任務爲:

private class ImageTransfer extends AsyncTask<Void,Void,Bitmap> { 

    public Bitmap bitmap1 = null; 
    ImageView im_v = null; 
    public ImageTransfer(ImageView view){ 
     this.im_v= view; 
    } 
    @Override 
    protected Bitmap doInBackground(Void... params) { 
     try { 
      bitmap1 = doItOnServer(); 
     } catch (Exception e) { 
      Log.v("Exception", e.getMessage()); 
     } 
     return bitmap1; 
    } 

當我嘗試在異步任務中顯示圖像時,它會拋出空指針異常

protected void onPostExecute(Bitmap result) { 
     try { 
      if (bitmap1 != null) { 
       im_v.setImageBitmap(result); 
      } else { 
       Log.v("Error reading bitmap", "error"); 
      } 
     } catch (Exception e) { 
      Log.e("OnPost Exec", "Exception", e); 
     } 
    } 

日誌中捕獲的捕獲的異常是:

3166-3166/junaidyounas.makerprojectjava E/OnPost Exec﹕ Exception 
java.lang.NullPointerException 
     at junaidyounas.makerprojectjava.OpenCVSelecter$ImageTransfer.onPostExecute(OpenCVSelecter.java:639) 
     at junaidyounas.makerprojectjava.OpenCVSelecter$ImageTransfer.onPostExecute(OpenCVSelecter.java:615) 
     at android.os.AsyncTask.finish(AsyncTask.java:677) 
     at android.os.AsyncTask.access$600(AsyncTask.java:177) 
     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:690) 
     at android.os.Handler.dispatchMessage(Handler.java:102) 
     at android.os.Looper.loop(Looper.java:149) 
     at android.app.ActivityThread.main(ActivityThread.java:5045) 
     at java.lang.reflect.Method.invokeNative(Native Method)..... 

回答

0

我只是固定代碼,但我認爲你應該POST方法doItOnServer():

protected void onPostExecute(Bitmap result) { 
     try { 
      if (result != null) { 
       im_v.setImageBitmap(result); 
      } else { 
       Log.v("Error reading bitmap", "error"); 
      } 
     } catch (Exception e) { 
      Log.e("OnPost Exec", "Exception", e); 
     } 
    } 
+0

我收到的圖像doItOnServer和我在那裏做我已經發布....仍然無法正常工作 –