[j2me入门] 求助关于MIDletStateChangeException 异常的解释?

zhieer 2006-12-14
/*
* MidletTest.java
*
* Created on 2006年12月13日, 下午8:13
*/

package test;

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

/**
*
* @author Administrator
*/
public class MidletTest extends MIDlet implements CommandListener, Runnable{

    private Form form;
    private Command quit;
    private boolean forceExit = false;
   
    /** Creates a new instance of MidletTest */
    public MidletTest() {
        System.out.print("Constructor called.");
        form = new Form("Life,Jim.");
        form.append("But not as we know it.");
        form.setCommandListener(this);
       
        quit = new Command("Quit",Command.SCREEN,1);
        form.addCommand(quit);
    }
   
//GEN-BEGIN:MVDFields
    private static final int _ASYNC_NOTHING = 0;
    private int _asyncMethod = _ASYNC_NOTHING;
    private Object[] _asyncParameters = new Object[2];//GEN-END:MVDFields
   
//GEN-BEGIN:MVDMethods
// WARNING - "Generate Threaded Command Listeners" document property is deprecated - use WaitScreen components instead.
// It will NOT be possible to open/import this file in the future.
   
    /** This method is run in another thread and communicates with event handlers and provides ability for event processing in different thread.
     **/
    public void run() {
        int asyncMethod;
        Object firstParameter;
        Object secondParameter;
       
        for (;;) {
            // wait and get request for event processing
            synchronized (_asyncParameters) {
                _asyncMethod = _ASYNC_NOTHING;
                _asyncParameters[0] = null;
                _asyncParameters[1] = null;
                try {
                    _asyncParameters.wait();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                asyncMethod = _asyncMethod;
                firstParameter = _asyncParameters[0];
                secondParameter = _asyncParameters[1];
            }
           
        }
    }//GEN-END:MVDMethods

    /** This method initializes UI of the application.//GEN-BEGIN:MVDInitBegin
     */
    private void initialize() {//GEN-END:MVDInitBegin
        // Insert pre-init code here
        new java.lang.Thread(this).start();//GEN-LINE:MVDInitInit
        // Insert post-init code here
    }//GEN-LINE:MVDInitEnd
   
    /**
     * This method should return an instance of the display.
     */
    public Display getDisplay() {//GEN-FIRST:MVDGetDisplay
        return Display.getDisplay(this);
    }//GEN-LAST:MVDGetDisplay
   
    /**
     * This method should exit the midlet.
     */
    public void exitMIDlet() {//GEN-FIRST:MVDExitMidlet
        getDisplay().setCurrent(null);
        destroyApp(true);
        notifyDestroyed();
    }//GEN-LAST:MVDExitMidlet
   
    public void startApp() {
         System.out.println("startApp() called.");
         Display.getDisplay(this).setCurrent(form);
    }
   
    public void pauseApp() {
        System.out.println("pauseApp() called.");
    }
   
    public void destroyApp(boolean unconditional) {
        System.out.println("destroyApp(" + unconditional + ") called.");
        forceExit = true;
    }

    public void commandAction(Command command, Displayable displayable) {
        System.out.println("commandAction(" + command + "," + command.getLabel() + ") called.");
        try{
            if(command == quit){
                destroyApp(forceExit);
                notifyDestroyed();
            }
        }catch(MIDletStateChangeException me){
        }
    }
}
在最后一句里catch(MIDletStateChangeException me)可以这样用吗?
wuhua 2006-12-15
关于它的解释看下api文档:
Signals that a requested MIDlet state change failed. This exception is thrown by the MIDlet in response to state change calls into the application via the MIDlet interface

Signals the MIDlet to terminate and enter the Destroyed state. In the destroyed state the MIDlet must release all resources and save any persistent state. This method may be called from the Paused or Active states.

MIDlets should perform any operations required before being terminated, such as releasing resources or saving preferences or state.

Note: The MIDlet can request that it not enter the Destroyed state by throwing an MIDletStateChangeException. This is only a valid response if the unconditional flag is set to false. If it is true the MIDlet is assumed to be in the Destroyed state regardless of how this method terminates. If it is not an unconditional request, the MIDlet can signify that it wishes to stay in its current state by throwing the MIDletStateChangeException. This request may be honored and the destroy() method called again at a later time.

If a Runtime exception occurs during destroyApp then they are ignored and the MIDlet is put into the Destroyed state.

参数:
unconditional If true when this method is called, the MIDlet must cleanup and release all resources. If false the MIDlet may throw MIDletStateChangeException to indicate it does not want to be destroyed at this time.
抛出:
MIDletStateChangeException is thrown if the MIDlet wishes to continue to execute (Not enter the Destroyed state). This exception is ignored if unconditional is equal to true.

Signals the MIDlet that it has entered the Active state. In the Active state the MIDlet may hold resources. The method will only be called when the MIDlet is in the Paused state.

Two kinds of failures can prevent the service from starting, transient and non-transient. For transient failures the MIDletStateChangeException exception should be thrown. For non-transient failures the notifyDestroyed method should be called.

If a Runtime exception occurs during startApp the MIDlet will be destroyed immediately. Its destroyApp will be called allowing the MIDlet to cleanup.

抛出:
MIDletStateChangeException is thrown if the MIDlet cannot start now but might be able to start at a later time.


try{
if(command == quit){
destroyApp(forceExit);
notifyDestroyed();
}
}catch(MIDletStateChangeException me){
}
}
} 在最后一句里catch(MIDletStateChangeException me)可以这样用吗?
这样不可以,因为你访问的方法并没有throw这个异常

zhieer 2006-12-16
我是看、《j2me 手机游戏开发》书里前面有这么一段,感觉也很奇怪,看来尽信书不如无书。
Global site tag (gtag.js) - Google Analytics