2017-07-10 66 views
0

我正在根據照片類型使用2個佈局製作列表視圖。如果照片是方形的,則使用方形照片佈局,否則使用矩形,以及其他屬性。我試圖解決這個問題,但我無法弄清楚爲什麼我得到NullPointerException,即使我已經初始化ImageView。以下是我的代碼:Android嘗試調用虛擬方法

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 

LayoutInflater inflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

View row; 
final CartPhoto cp = photos.get(position); 

if (convertView == null) { 
    if(cp.image.width != cp.image.height){ // Check for not Square Photo 
     row = inflater.inflate(layoutId, null); 
    } 
    else 
     row = inflater.inflate(layoutId_square, null); // Square Photo 

} else { 
    row = convertView; 
} 

final ImageView iv = (ImageView) row.findViewById(R.id.sq_image); 
final ImageView iv_sqr = (ImageView) row.findViewById(R.id.sq_image_square); 

float finalAspect = Product.aspectForProductSize(Product.ProductSize.Size4x6); 
Bitmap thumb = cp.image.getThumbnail(context); 

if(thumb != null) { 
    if (thumb.getWidth() < thumb.getHeight()) { // portrait 
     thumb = ImageOperations.cropToAspect(thumb, finalAspect); 
    } else { 
     thumb = ImageOperations.cropToAspect(thumb, 1/finalAspect); 
    } 


    if(cp.image.width != cp.image.height){ 

     iv.setImageBitmap(thumb);   // ERROR 1 
     iv.setOnClickListener(imageClicked); 
    } 
    else { 

     iv_sqr.setImageBitmap(thumb);  // ERROR 2 
     iv_sqr.setOnClickListener(imageClicked); 
    } 
} 
else { 
    // Crashlytics Log 
    //String msg = "Else - Thumbnail Width : " + cp.image.width + ", Thumbnail Height : " + cp.image.height + "\n URI: " + cp.image.fullUrl; 
    //Crashlytics.log(msg); 
} 

/*** Square or Rectangular ***/ 
    /* 
    if(cp.image.width == cp.image.height) {  // SQUARE 

     setupSizeQuantityButton(row, R.id.sq_4x4_minus, R.id.sq_4x4_qty, Product.ProductSize.Size4x4, -1, cp); 
     setupSizeQuantityButton(row, R.id.sq_4x4_plus, R.id.sq_4x4_qty, Product.ProductSize.Size4x4, 1, cp); 
     setupQuantityLabel(row, R.id.sq_4x4_qty, Product.ProductSize.Size4x4, cp); 

     setupSizeQuantityButton(row, R.id.sq_8x8_minus, R.id.sq_8x8_qty, Product.ProductSize.Size8x8, -1, cp); 
     setupSizeQuantityButton(row, R.id.sq_8x8_plus, R.id.sq_8x8_qty, Product.ProductSize.Size8x8, 1, cp); 
     setupQuantityLabel(row, R.id.sq_8x8_qty, Product.ProductSize.Size8x8, cp); 
    } 
    else { 
     setupSizeQuantityButton(row, R.id.sq_4x6_minus, R.id.sq_4x6_qty, Product.ProductSize.Size4x6, -1, cp); 
     setupSizeQuantityButton(row, R.id.sq_4x6_plus, R.id.sq_4x6_qty, Product.ProductSize.Size4x6, 1, cp); 
     setupQuantityLabel(row, R.id.sq_4x6_qty, Product.ProductSize.Size4x6, cp); 

     setupSizeQuantityButton(row, R.id.sq_5x7_minus, R.id.sq_5x7_qty, Product.ProductSize.Size5x7, -1, cp); 
     setupSizeQuantityButton(row, R.id.sq_5x7_plus, R.id.sq_5x7_qty, Product.ProductSize.Size5x7, 1, cp); 
     setupQuantityLabel(row, R.id.sq_5x7_qty, Product.ProductSize.Size5x7, cp); 

     setupSizeQuantityButton(row, R.id.sq_8x10_minus, R.id.sq_8x10_qty, Product.ProductSize.Size8x10, -1, cp); 
     setupSizeQuantityButton(row, R.id.sq_8x10_plus, R.id.sq_8x10_qty, Product.ProductSize.Size8x10, 1, cp); 
     setupQuantityLabel(row, R.id.sq_8x10_qty, Product.ProductSize.Size8x10, cp); 
    } 

return row; 
} 

我收到錯誤的行分別註釋爲錯誤1和錯誤2。以下是我的錯誤日誌:

Fatal Exception: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageBitmap(android.graphics.Bitmap)' on a null object reference 
    at com.mailpix.samedayphoto.adapters.SizeQuantityAdapter.getView(SizeQuantityAdapter.java:128) 
    at android.widget.AbsListView.obtainView(AbsListView.java:2347) 
    at android.widget.GridView.makeAndAddView(GridView.java:1433) 
    at android.widget.GridView.makeRow(GridView.java:361) 
    at android.widget.GridView.fillDown(GridView.java:302) 
    at android.widget.GridView.fillGap(GridView.java:262) 
    at android.widget.AbsListView.trackMotionScroll(AbsListView.java:4995) 
    at android.widget.AbsListView$FlingRunnable.run(AbsListView.java:4543) 
    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:773) 
    at android.view.Choreographer.doCallbacks(Choreographer.java:586) 
    at android.view.Choreographer.doFrame(Choreographer.java:555) 
    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:759) 
    at android.os.Handler.handleCallback(Handler.java:739) 
    at android.os.Handler.dispatchMessage(Handler.java:95) 
    at android.os.Looper.loop(Looper.java:135) 
    at android.app.ActivityThread.main(ActivityThread.java:5333) 
    at java.lang.reflect.Method.invoke(Method.java) 
    at java.lang.reflect.Method.invoke(Method.java:372) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:940) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:735) 
+0

你有兩個ImageView的「sq_image'和 'sq_image_square'。我相信這是兩種不同的佈局。因此,根據您膨脹的佈局,與其他佈局關聯的圖像視圖將爲空。您可能想要移動代碼以在條件中獲取圖像視圖。 – Jimmy

回答

0
final ImageView iv = (ImageView) row.findViewById(R.id.sq_image); 
final ImageView iv_sqr = (ImageView) row.findViewById(R.id.sq_image_square); 

在這裏,我想你已經分別兩種不同的佈局長方形和正方形圖像。現在檢查每個佈局是否只有一個圖像視圖ID(R.id.sq_imageR.id.sq_image_square)。

+0

我有2種不同的矩形和方形圖像佈局。另外,我有兩個不同的ImageView ID(R.id.sq_image和R.id.sq_image_square)。 – Vishal

+0

如果你可以分享你的佈局代碼,最好找到解決方案... – mustafiz012

+0

你可以找到代碼(https://github.com/vishalmusale/StackOverflow) – Vishal

0

嘗試將其移至相應的ifs。

if (convertView == null) { 
    if(cp.image.width != cp.image.height){ // Check for not Square Photo 
     row = inflater.inflate(layoutId, null); 
     ImageView iv = (ImageView) row.findViewById(R.id.sq_image); 
    } 
    else 
     row = inflater.inflate(layoutId_square, null); // Square Photo 
     ImageView iv_sqr = (ImageView) row.findViewById(R.id.sq_image_square);  
} else { 
    row = convertView; 
} 

我實例化的ImageView略低於LayoutInflater,像這樣:

LayoutInflater inflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
ImageView iv; 
ImageView iv_sqr; 
相關問題