2011-08-01 52 views
0

我在我的屏幕添加三個位圖。圖像需要改變與焦點和unfocus.Now如果我在我的自定義方法內聲明位圖,比wheni從一個圖像滾動到另一個,它會給null指針異常。但是當我在方法外部聲明位圖時,我沒有得到任何異常,但只有最後一個聚焦圖像集中到處,但它應該是像三個圖像三個單獨聚焦的圖像在那裏。下面是我的代碼。請幫忙。黑莓bitmapfield焦點unfocus問題

private BitmapField getBitmapField(final Item item, final int cellWid, final int cellHgt, final long style) { 
     final Bitmap bitmap = Bitmap.getBitmapResource(item.imgUrl); 
     final Bitmap bitmapfoc = Bitmap.getBitmapResource(item.imgUrlimp); 
     BitmapField bitmapField = new BitmapField(bitmap, style) { 
      boolean _inFocus = false; 

      protected void onFocus(int direction) { 
       _inFocus = true; 

       selectedIndex = flowFieldManager.getFieldWithFocusIndex(); 
       System.out.println("Selected Index :"+selectedIndex); 
       if(TextControl.labelField != null) 
       TextControl.labelField.setText(item.title); 

       super.onFocus(direction); 
       //this.invalidate(); 
      } 
      protected void onUnfocus() { 
       _inFocus = false; 
       super.onUnfocus(); 
       //this.invalidate(); 
      } 
      public void paint(Graphics graphics) { 
       System.out.println("====barView=== :"+barview); 

      graphics.drawBitmap(0, 0, bitmap.getWidth(),bitmap.getHeight(), bitmap, 0, 0); //draw bachground image bitmap 
      invalidate(); 
       //super.paint(graphics); 
       } 
      protected void drawFocus(Graphics g, boolean arg1) { 

     g.drawBitmap(0,0, bitmapfoc.getWidth(), bitmapfoc.getHeight(), bitmapfoc, 0, 0); //draw bachground image bitmap 
      invalidate(); 
      } 
+2

不相關的問題,但你不應該叫'無效()'從畫法的內部自'無效()'表示它需要重新繪製。你能否給我們提供任何有用的信息,比如NPE在哪裏被拋出? – jprofitt

回答

1
BitmapField chaneBitmap(String image1,String image2){ 
    final Bitmap original= Bitmap.getBitmapResource(image1); 
    final Bitmap change = Bitmap.getBitmapResource(image2); 
    BitmapField _hold_bitmap=new BitmapField(original,BitmapField.FOCUSABLE){  
     protected void drawFocus(Graphics graphics, boolean on){} 
      public void onFocus(int direction){ 
        invalidate(); 
        setBitmap(change); 
      } 

      public void onUnfocus(){ 
        super.onUnfocus(); 
        setBitmap(original); 
      } 
     } 
} 
+0

在這種方法中,您給出了兩個圖像名稱一個用於原始和其他用於更改.drawFocus()刪除焦點的原始效果.Onfocus提供更改圖像並將焦點放在原始圖像上..享受.... – Blackberry