0
votes

I am adding three bitmap in my screen.The image needs to change with focus and unfocus.Now if i declare the bitmaps inside my customised method,than wheni scroll from one image to another,it gives null pointer exception.But when i declare the bitmaps outside the method,i dont get any exception,but only the last focused image gets focused everywhere,but it should be like that for three images three seperate focused images are there.Below is my code.Please help.

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();
            }
1
Not related to your problem, but you shouldn't call invalidate() from inside of a painting method since invalidate() signifies it needs to repaint. Can you give us any helpful info, like where the NPE is getting thrown? - jprofitt

1 Answers

1
votes
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);
             }
        }
}