【发布时间】:2011-12-02 15:10:15
【问题描述】:
我在我的屏幕上添加三个位图。图像需要随着焦点和不聚焦而改变。现在如果我在我的自定义方法中声明位图,那么当我从一个图像滚动到另一个图像时,它会给出空指针异常。但是当我在方法之外声明了位图,我没有得到任何异常,但是只有最后一个聚焦的图像在所有地方都聚焦,但是对于三个图像,三个单独的聚焦图像应该是这样的。下面是我的代码。请帮忙。
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();
}
【问题讨论】:
-
与您的问题无关,但您不应从绘画方法内部调用
invalidate(),因为invalidate()表示它需要重新绘画。您能给我们提供任何有用的信息,例如 NPE 被抛出的位置吗?
标签: blackberry java-me