IllegalStateException黑莓 [英] IllegalStateException BlackBerry

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

问题描述

public class show extends MainScreen {
    private String date1;
    private long date1l;
    private long date2l;
    private LabelField curDate = new LabelField();
    private LabelField toDate = new LabelField();
    private LabelField diffe = new LabelField();
    // private LabelField info;
    // private LabelField empty;
    // private InvokeBrowserHyperlinkField hello;

    ButtonField activate = null;
    ButtonField disactivate = null;
    Timer timer;
    Timer timer2;

    public String date1s[];
    int d, m, y;
    int x = 1;
    String day, hour, minute;
    Date date = new Date();

    Calendar calendar = Calendar.getInstance();;
    SimpleDateFormat dateFormat = new SimpleDateFormat("dd MM yyyy HH mm");

    public show() {
        add(curDate);
        add(toDate);
        add(new SeparatorField());
        add(diffe);

        timer = new Timer();
        timer.scheduleAtFixedRate(new TimerTick(), 0, 1000);
    }

    private class TimerTick extends TimerTask {
        public void run() {
            if (x != 0) {
                date1l = date.getTime();

                try {
                    date1 = dateFormat.format(calendar.getTime());
                } catch (Exception e) {
                    e.printStackTrace();
                }

                Calendar cal = Calendar.getInstance();
                cal.setTimeZone(TimeZone.getTimeZone("GMT+7:00"));
                cal.set(Calendar.HOUR_OF_DAY, 0);
                cal.set(Calendar.MINUTE, 0);
                cal.set(Calendar.MONTH, 1);
                cal.set(Calendar.DATE, 1);
                cal.set(Calendar.YEAR, 2012);

                date2l = cal.getTime().getTime();

                date1s = StringUtilities.stringToWords(date1);
                d = Integer.parseInt(date1s[0]);
                m = Integer.parseInt(date1s[1]);
                y = Integer.parseInt(date1s[2]);
                display();
            } else {
                timer.cancel();
            }
        }
    }

    public void display() {
        String monw = convertToWords(m);
        curDate.setText("Current Date = " + d + " " + monw + " " + y + " "
                + date1s[3] + ":" + date1s[4]);
        toDate.setText("To Date = 1 February 2012 00:00");

        long diffms = date2l - date1l;
        long ds = diffms / 1000;
        long dm = ds / 60;
        long dh = dm / 60;
        long dd = dh / 24;

        long q = dd;
        long h = (ds - (dd * 24 * 60 * 60)) / (60 * 60);
        long m = (ds - (dh * 60 * 60)) / 60;

        diffe.setText("Remaining Time : \n" + Long.toString(q) + " day(s) "
                + Long.toString(h) + " hour(s) " + Long.toString(m)
                + " minute(s)");

        day = Long.toString(q);
        hour = Long.toString(h);
        minute = Long.toString(m);

        showMessage();
    }

    /*
     * private void link() { empty = new LabelField("\n\n"); add(empty); hello =
     * new InvokeBrowserHyperlinkField("Click here",
     * "http://indri.dedicated-it.com/wordpress/?page_id=17"); add(hello); info
     * = new LabelField("\n\nPress menu then choose \"Get Link\" to access");
     * add(info); }
     */

    void showMessage() {
        activate = new ButtonField("Activate", FIELD_HCENTER) {
            protected boolean navigationClick(int action, int time) {
                if (activate.isFocus()) {
                    Dialog.alert("Started!!");
                    Start();
                }
                return true;
            }
        };
        add(activate);

        disactivate = new ButtonField("Disactivate", FIELD_HCENTER) {
            protected boolean navigationClick(int action, int time) {
                if (disactivate.isFocus()) {
                    Dialog.alert("Stopped!!");
                    timer2.cancel();
                }
                return true;
            }
        };
        add(disactivate);

        /*
         * UiEngine ui = Ui.getUiEngine(); Screen screen = new
         * Dialog(Dialog.D_OK, data, Dialog.OK,
         * Bitmap.getPredefinedBitmap(Bitmap.EXCLAMATION),
         * Manager.VERTICAL_SCROLL); ui.queueStatus(screen, 1, true);
         */
    }

    public void Start() {
        timer2 = new Timer();

        timer2.scheduleAtFixedRate(new TimerTick2(), 0, 6000);
    }

    private class TimerTick2 extends TimerTask {

        public void run() {

            UiApplication.getUiApplication().invokeLater(new Runnable() {
                public void run() {
                    synchronized (Application.getEventLock()) {
                        UiEngine ui = Ui.getUiEngine();
                        Screen screen = new Dialog(Dialog.D_OK, "Hello!",
                                Dialog.OK,
                                Bitmap.getPredefinedBitmap(Bitmap.EXCLAMATION),
                                Manager.VERTICAL_SCROLL);
                        ui.pushGlobalScreen(screen, 1, UiEngine.GLOBAL_QUEUE);
                    }
                }

            });

        }
    }

    private String convertToWords(int m) {
        String w = "";
        switch (m) {
        case 1:
            w = "January";
            break;
        case 2:
            w = "February";
            break;
        case 3:
            w = "March";
            break;
        case 4:
            w = "April";
            break;
        case 5:
            w = "May";
            break;
        case 6:
            w = "June";
            break;
        case 7:
            w = "July";
            break;
        case 8:
            w = "August";
            break;
        case 9:
            w = "September";
            break;
        case 10:
            w = "October";
            break;
        case 11:
            w = "November";
            break;
        case 12:
            w = "December";
            break;
        }
        return w;
    }

    public boolean onClose() {
        UiApplication.getUiApplication().requestBackground();
        return true;
    }
}

什么是JVM 104 IllegalStateException?该计划是一个倒计时程序,计数从今天到2月1日的剩余时间。此外,即使应用程序关闭,我也实现了一个定时器功能。你能帮我找个问题吗?谢谢

What actually is JVM 104 IllegalStateException? This program is a countdown program, which counts the remaining time from today until February 1st. Also, I implement a timer function that appears even if the application is closed. Can u please help me locate the problem? Thank you

推荐答案

Richard 说,你正在尝试从另一个线程更新LabelField。尝试以下代码片段:

As Richard said, you are trying to update LabelField from another thread. Try the following code snippet:

synchronized (UiApplication.getEventLock()) {
    labelField.setText();
}

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

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