命中ClassCastException

Ala*_*Lai 1 blackberry popup classcastexception

我打电话给这个班级PopupScreen.

public class Custom_LoadingScreen extends PopupScreen {
private VerticalFieldManager vfm;
private Util_AnimateGifField anmtFldCycle = null;
private GIFEncodedImage gifImgCycle;

public Custom_LoadingScreen() {
    super(new VerticalFieldManager());
    Background bg = BackgroundFactory.createSolidTransparentBackground(
            Color.BLACK, 190);
    setBackground(bg);
    setBorder(BorderFactory.createSimpleBorder(new XYEdges(),
            Border.STYLE_TRANSPARENT));

    gifImgCycle = (GIFEncodedImage) GIFEncodedImage
            .getEncodedImageResource("LoadingSpinner.gif");
    anmtFldCycle = new Util_AnimateGifField(gifImgCycle,
            Field.FIELD_HCENTER);

    vfm = new VerticalFieldManager(USE_ALL_WIDTH) {
        protected void sublayout(int maxWidth, int maxHeight) {
            super.sublayout(Display.getWidth(), Display.getHeight());
            setExtent(Display.getWidth(), Display.getHeight());
        }
    };
    int padding = (Display.getHeight() - 16) / 2;
    if (padding > 0) {
        anmtFldCycle.setPadding(padding, 0, 0, 0);
    }
    vfm.add(anmtFldCycle);
    add(vfm);
}

//public void Popupscreen() {
    //Main.getUiApplication().popScreen(this);
//}

public boolean keyDown(int keycode, int status) {
    if (Keypad.key(keycode) == Keypad.KEY_ESCAPE) {
        Main.getUiApplication().popScreen(this);
        return true;
    }
    return super.keyDown(keycode, status);
}
}
Run Code Online (Sandbox Code Playgroud)

在按钮中,我按下它然后转到下一个屏幕.

financebtn = new Custom_ButtonField(finance, financeactive,
            financeactive) {
        protected boolean navigationClick(int status, int time) {
            Main.getUiApplication().pushScreen(new Custom_LoadingScreen());
            Main.getUiApplication().invokeLater(new Runnable() {
                public void run() {
                //  Main.getUiApplication().popScreen();
                    Main.getUiApplication().pushScreen(
                            new Main_NewsDetail());
                }

            }, 1 * 1000, false);

            return true;
        }
    };
    add(financebtn);
Run Code Online (Sandbox Code Playgroud)

结果给了我Uncaught:ClassCastException.我可以调用另一个类似于custom_loadingscreenpopupscreen的类.它工作正常.

我也试过在另一个按钮中调用这个类,但仍然是同样的问题.

Nat*_*ate 5

如果你看看你的Custom_LoadingScreen代码,你只有一个地方在做一个演员:

gifImgCycle = (GIFEncodedImage) GIFEncodedImage
        .getEncodedImageResource("LoadingSpinner.gif");
Run Code Online (Sandbox Code Playgroud)

所以,这是一个开始寻找的好地方.如果你谷歌为"BlackBerry GIFEncodedImage ClassCastException",你会发现这个线程:

http://supportforums.blackberry.com/t5/Java-Development/GIFEncodedImage-in-BlackBerry-OS7/td-p/1228959

问题是,为了优化,BlackBerry喜欢将图像转换为PNG格式,而大多数智能手机最适合这种格式.所以,这里发生的是你的GIF图像实际上被转换为PNG图像.因此,当您调用该getEncodedImageResource()方法时,您要返回的对象实际上可能是类型PNGEncodedImage,而不是GIFEncodedImage,并且您获得了异常.偷偷摸摸,是吗?

你可以通过几种方式解决它.

  1. Blackberry_App_Descriptor.xml文件中,您可以取消选中指定图像转换为PNG的设置(" 构建"选项卡 - >" 将图像文件转换为png")
  2. 您可以通过将GIF文件重命名为LoadingSpinner.agif来欺骗构建系统.该工具集无法识别.agif扩展名,因此不会尝试转换它.当然,如果这样做,请记住在加载Java代码时也要更改它.
  3. 您可以更改要使用的代码PNGEncodedImage,或者像这样测试对象:
EncodedImage img = EncodedImage.getEncodedImageResource("LoadingSpinner.gif");
if (img instanceof GIFEncodedImage) {
   // cast to GIFEncodedImage
} else if (img instanceof PNGEncodedImage) {
   // cast to PNGEncodedImage
}
Run Code Online (Sandbox Code Playgroud)

数字(1)将失去所有非PNG图像的非PNG到PNG转换优化,而不仅仅是这个.

数字(2)确实看起来有点难看.但是,这样做的好处是,您可以仅为这一个图像禁用此行为.如果您的大部分图像都不是PNG图像,那么让BlackBerry为您优化其他图像可能很有价值.但是,也许这个人需要成为GIF.所以,#2允许你作为一个特例来处理这个.

我只是猜测这张图片可能是动画 GIF吗?是对的吗?如果是这样,您可能希望将其保留为GIF,因此您不希望使用数字(3),这可以将其转换为PNG,并将其用作此类.

  • @AlanLai.别客气.请注意:除了接受答案之外,你现在有足够的积分来支持Upvote(每个答案旁边的箭头按钮).理想情况下,你也会这样做.事实上,如果你的一个问题得到两个答案,并且它们都是有用的,你可以对它们进行双向投票,然后只接受你认为最好的那个.即使你没有投票,我会继续尝试回答你的问题,但我只是提到它:) (3认同)