打ClassCastException异常 [英] Hit ClassCastException

查看:260
本文介绍了打ClassCastException异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我调用这个类,这是 PopupScreen

公共类Custom_LoadingScreen扩展PopupScreen {
私人VerticalFieldManager VFM;
私人Util_AnimateGifField anmtFldCycle = NULL;
私人GIFEn codeDIMAGE gifImgCycle;公共Custom_LoadingScreen(){
    超级(新VerticalFieldManager());
    背景BG = BackgroundFactory.createSolidTransparentBackground(
            Color.BLACK,190);
    的setBackground(BG);
    setBorder(BorderFactory.createSimpleBorder(新XYEdges(),
            Border.STYLE_TRANSPARENT));    gifImgCycle =(GIFEn codeDIMAGE)GIFEn codeDIMAGE
            .getEn codedImageResource(LoadingSpinner.gif);
    anmtFldCycle =新Util_AnimateGifField(gifImgCycle,
            Field.FIELD_HCENTER);    VFM =新VerticalFieldManager(USE_ALL_WIDTH){
        保护无效sublayout(INT了maxWidth,诠释了maxHeight){
            super.sublayout(Display.getWidth(),Display.getHeight());
            setExtent(Display.getWidth(),Display.getHeight());
        }
    };
    INT填充=(Display.getHeight() - 16)/ 2;
    如果(填充大于0){
        anmtFldCycle.setPadding(填充,0,0,0);
    }
    vfm.add(anmtFldCycle);
    加(VFM);
}//公共无效Popupscreen(){
    //Main.getUiApplication().popScreen(this);
//}公共布尔KEYDOWN(INT键code,诠释状态){
    如果(Keypad.key(键code)== Keypad.KEY_ESCAPE){
        。Main.getUiApplication()popScreen(本);
        返回true;
    }
    返回super.keyDown(键code,地位);
}
}

在一个按钮,我把它转到下一屏幕前。

financebtn =新Custom_ButtonField(金融,financeactive,
            financeactive){
        保护布尔navigationClick(INT的地位,诠释时间){
            。Main.getUiApplication()pushScreen(新Custom_LoadingScreen());
            Main.getUiApplication()的invokeLater(Runnable的新(){
                公共无效的run(){
                // Main.getUiApplication()popScreen();
                    Main.getUiApplication()。pushScreen(
                            新Main_NewsDetail());
                }            },1 * 1000,FALSE);            返回true;
        }
    };
    加(financebtn);

结果给我未捕获:抛出ClassCastException 。我可以调用另一个类,这是类似于 custom_loadingscreen 也popupscreen。它做工精细。

我也试过调用这个类中的其他按钮,但仍同样的问题。


解决方案

如果你看看你的 Custom_LoadingScreen code,但只有一个你所在的地方做投:

gifImgCycle =(GIFEn codeDIMAGE)GIFEn codeDIMAGE
        .getEn codedImageResource(LoadingSpinner.gif);

所以,这是开始寻找一个好地方。如果你对谷歌的黑莓GIFEn codeDIMAGE ClassCastException异常后,你会发现这个线程:

<一个href=\"http://supportforums.blackberry.com/t5/Java-Development/GIFEn$c$cdImage-in-BlackBerry-OS7/td-p/1228959\">http://supportforums.blackberry.com/t5/Java-Development/GIFEn$c$cdImage-in-BlackBerry-OS7/td-p/1228959

的问题在于,对于优化,黑莓喜欢将影像转换为PNG格式,其中大部分智能工作最好用。因此,这里发生了什么是你的GIF图片实际上是被转换成一个PNG图像。因此,当你调用 getEn codedImageResource()方法,对象你找回实际上可能是类型 PNGEn codeDIMAGE ,而不是 GIFEn codeDIMAGE ,你得到的异常。偷偷摸摸的,是吧?

您可以解决这个问题的一些方法。


  1. Blackberry_App_Descriptor.xml 文件,可以取消,指定图像转换为PNG(设置的生成标签 - > 图像文件转换为PNG

  2. 您可以通过重命名你的GIF文件,以类似的 LoadingSpinner.agif 欺骗构建系统。该工具不能识别.agif延伸,因此不会尝试将其转换。如果你这样做,当然,记得要改变文件名在Java code,也装载时。

  3. 您可以更改code使用 PNGEn codeDIMAGE ,或测试对象是这样的:

恩codeDIMAGE IMG = EN codedImage.getEn codedImageResource(LoadingSpinner.gif) ;
如果(IMG的instanceof GIFEn codeDIMAGE){
   //投地GIFEn codeDIMAGE
}否则如果(IMG的instanceof PNGEn codeDIMAGE){
   //投地PNGEn codeDIMAGE
}

数(1)将失去非PNG至PNG转换优化所有非PNG图像,不只是这一个。

数目(2)看起来有点难看。这样做的好处,不过,是你可以禁用仅此一图像此行为。如果你的大多数图像都没有PNG图像,这可能是有价值的,让黑莓手机优化的你,对其他影像。但是,也许这其中需要一个GIF。因此,#2,您可以处理这其中的一个特例。

我只是猜测,这个形象可能是一个动画 GIF?是对的吗?如果是这样,你可能想保持它作为一个GIF,这样你就不会想要做的数目(3),它可以被转换成一个PNG,并使用它作为这样的。

I am calling this class which is 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);
}
}

In a button, I pushed it before goes to next screen.

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);

The result give me Uncaught:ClassCastException. I can call another class which is similar to custom_loadingscreen also popupscreen. It work fine.

I also tried call this class in another button yet still same problem.

解决方案

If you take a look at your Custom_LoadingScreen code, there's only one place where you are doing a cast:

gifImgCycle = (GIFEncodedImage) GIFEncodedImage
        .getEncodedImageResource("LoadingSpinner.gif");

So, that's a good place to start looking. If you Google for "BlackBerry GIFEncodedImage ClassCastException", you'll find this thread:

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

The problem is that, for optimization, BlackBerry likes to convert images to the PNG format, which most smartphones work best with. So, what's happening here is that your GIF image is actually being converted to a PNG image. Therefore, when you call the getEncodedImageResource() method, the object you are getting back may actually be of type PNGEncodedImage, not GIFEncodedImage, and you get the exception. Sneaky, huh?

You can solve it a few ways.

  1. In the Blackberry_App_Descriptor.xml file, you can uncheck the setting that specifies that images are converted to PNG (Build tab -> Convert image files to png)
  2. You can trick the build system by renaming your GIF file to something like LoadingSpinner.agif. The toolset doesn't recognize the .agif extension, and therefore won't try to convert it. If you do this, of course, remember to change the filename in your Java code, too, when loading it.
  3. You can change the code to use PNGEncodedImage, or test the object like this:

EncodedImage img = EncodedImage.getEncodedImageResource("LoadingSpinner.gif");
if (img instanceof GIFEncodedImage) {
   // cast to GIFEncodedImage
} else if (img instanceof PNGEncodedImage) {
   // cast to PNGEncodedImage
}

Number (1) will lose the non-PNG to PNG conversion optimization for all your non-PNG images, not just this one.

Number (2) does look a little ugly. The benefit of doing this, though, is that you can disable this behaviour for just this one image. If most of your images are not PNG images, it might be valuable to let BlackBerry optimize for you, for the other images. But, maybe this one needs to be a GIF. So, #2 lets you handle this one as a special case.

I'm just guessing that this image might be an animated GIF? Is that right? If so, you probably want to keep it as a GIF, so you won't want to do number (3), which lets it be converted to a PNG, and uses it as such.

这篇关于打ClassCastException异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆