首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2ME开发 >

帮忙看段程序解决办法

2012-02-23 
帮忙看段程序//书上的,但是我用wtk2.3运行后得不出应该的结果。应该是一开始点退出consule里会打印出因为un

帮忙看段程序
//书上的,但是我用wtk2.3运行后得不出应该的结果。应该是一开始点退出consule里会打印出因为unconditional为false的异常吧,然后再点一次才正常退出。
大家试试是什么结果
我怀疑wtk有问题。。特别是异常处理的问题

import   javax.microedition.midlet.*;
import   javax.microedition.lcdui.*;

/**
  *   A   MIDlet   which   demonstrates   the   lifecycle   of   a   MIDlet
  *   @author   Martin   J.   Wells
  */
public   class   LifecycleTest   extends   javax.microedition.midlet.MIDlet
                  implements   CommandListener
{
      private   Form   form;
      private   Command   quit;
      private   boolean   forceExit   =   false;

      /**
        *   Constructor   for   the   MIDlet   which   creates   a   simple   Form,   adds   some   text   and
        *   an   exit   command.   When   called   this   method   will   also   write   a   message   to   the
        *   console.   This   is   used   to   demonstrate   the   sequence   of   events   in   a   MIDlet 's
        *   lifecycle.
        */
      public   LifecycleTest()
      {
            System.out.println( "Constructor   called. ");

            form   =   new   Form( "Life,   Jim. ");
            form.append( "But   not   as   we   know   it. ");
            form.setCommandListener(this);

            //   Create   and   add   two   commands   to   change   the   MIDlet   state  
            quit   =   new   Command( "Quit ",   Command.SCREEN,   1);
            form.addCommand(quit);
      }

      /**
        *   Called   by   the   Application   Manager   when   the   MIDlet   is   starting   or   resuming
        *   after   being   paused.   In   this   example   it   acquires   the   current   Display   object
        *   and   uses   it   to   set   the   Form   object   created   in   the   MIDlet   constructor   as

        *   the   active   Screen   to   display.   Also   displays   a   message   on   the   console   when
        *   called   to   demonstrate   when   this   method   is   called   in   the   MIDlet   lifecycle.
        *   @throws   MIDletStateChangeException
        */
      protected   void   startApp()   throws   MIDletStateChangeException
      {


            System.out.println( "startApp()   called. ");
            Display.getDisplay(this).setCurrent(form);
      }

      /**
        *   Called   by   the   MID 's   Application   Manager   to   pause   the   MIDlet.   A   good
        *   example   of   this   is   when   the   user   receives   an   incoming   phone   call   whilst
        *   playing   your   game.   When   they 're   done   the   Application   Manager   will   call
        *   startApp   to   resume.   For   this   example   we   output   a   message   on   the   console
        *   to   indicate   when   this   method   is   called   in   the   MIDlet 's   lifecycle.
        */
      protected   void   pauseApp()
      {
            System.out.println( "pauseApp()   called. ");
      }

      /**
        *   Called   by   the   MID 's   Application   Manager   when   the   MIDlet   is   about   to
        *   be   destroyed   (removed   from   memory).   You   should   take   this   as   an   opportunity
        *   to   clear   up   any   resources   and   save   the   game.   For   this   example   we   output   a
        *   message   on   the   console   to   indicate   when   this   method   is   called   in   the
        *   MIDlet   lifecycle.
        *   @param   unconditional   if   false   you   have   the   option   of   throwing   a
        *   MIDletStateChangeException   to   abort   the   destruction   process.
        *   @throws   MIDletStateChangeException
        */
      protected   void   destroyApp(boolean   unconditional)   throws   MIDletStateChangeException
      {  
            System.out.println( "destroyApp( "   +   unconditional   +   ")   called. ");

            if   (!unconditional)
            {
                  //   we   go   through   once   using   unconditional,   next   time   it 's   forced.
                  forceExit   =   true;
            }
      }

      /**
        *   The   CommandListener   interface   method   called   when   the   user   executes   a


        *   Command,   in   this   case   it   can   only   be   the   quit   command   we   created   in   the
        *   constructor   and   added   to   the   Form.   We   also   output   a   console   message   when
        *   this   method   is   called.
        *   @param   command   the   command   that   was   triggered
        *   @param   displayable   the   displayable   on   which   the   event   occurred
        */
      public   void   commandAction(Command   command,   Displayable   displayable)
      {
            System.out.println( "commandAction( "   +   command   +   ",   "   +   displayable   +
                                                      ")   called. ");
            try
            {
                  if   (command   ==   quit)
                  {
                        destroyApp(forceExit);
                        notifyDestroyed();
                  }
            }

            catch   (MIDletStateChangeException   me)
            {
                  System.out.println(me   +   "   caught. ");
            }
      }
}


[解决办法]
destroyApp(FALSE)为正常退出。。。

[解决办法]
同意楼上的
正常退出用false,
强制退出用true,很可能会丢失信息,比如采用缓冲机制的的 rms在强制退出时就可能丢失

一般来讲,只用false就好了
[解决办法]
同意楼上的!!!!!!!

热点排行