当前位置: 代码迷 >> Java相关 >> J2ME问题
  详细解决方案

J2ME问题

热度:190   发布时间:2007-12-20 21:48:49.0
J2ME问题
请问我编译的一个程序,用模拟器运行时,一运行那模拟手机一闪而过,程序并没有问题啊??怎么回事呢,谢谢各位!

package Demo;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.TextBox;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

public class MMS extends MIDlet {
     private TextBox textbox; // TextBox 显示一条消息
     private Display disp; // 引用MIDlet的Display 对象
     private Command cmdExit; // 设定按钮用于退出MIDlet
     private Command cmdOK; // 确定按钮
     private Alert alt; // 信息提示对象

    public MMS() {
        super();
        disp = Display.getDisplay(this); // 获得当前MIDlet的Display对象
        cmdExit = new Command("退出", Command.SCREEN, 1); // 新建两个控制按钮
        cmdOK = new Command("阅读", Command.OK, 1);
        textbox = new TextBox("请输入待阅项目序号:", "", 40, 0); // 新建文本框
        textbox.addCommand(cmdExit); // 添加控制按钮
        textbox.addCommand(cmdOK);
        textbox.setCommandListener((CommandListener) this); // 开始侦听命令
    }

    protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
        // TODO Auto-generated method stub

    }

    protected void pauseApp() {
        // TODO Auto-generated method stub

    }

    protected void startApp() throws MIDletStateChangeException {
        alt = new Alert("整蛊专家 V1.0");// 开始运行时显示版权信息,新建信息框对象
        // 设置显示字符串
        alt.setString("==== 整蛊专家 V1.0 ====尹晓飞2007年作版权所有(C) 2007-2007");
        alt.setType(AlertType.INFO); // 设置为普通阅读信息框
        alt.setTimeout(Alert.FOREVER); // 信息窗口在按下DONE键后才能进入下一页面
        disp.setCurrent(alt, textbox); // 显示信息窗口

    }
    
    public void commandAction(Command arg0, Displayable arg1) throws MIDletStateChangeException {
    if (arg0 == cmdExit) { // 按下退出键时停止运行
       destroyApp(false);
       notifyDestroyed();
    }
    if (arg0 == cmdOK) { // 按下阅读键后阅读对应信息
        TextBox textbox = (TextBox)arg1; // 得到用户输入的内容
        String sInfo = textbox.getString();
    if (sInfo.equals("1") || sInfo.equals("2") || sInfo.equals("3") || sInfo.equals("4") || sInfo.equals("5")) { // 显示项目1的内容
    // 根据所选项目选择要显示的内容
    if (sInfo.equals("1")) alt.setString("一兄");
    if (sInfo.equals("2")) alt.setString("猪");
    if (sInfo.equals("3")) alt.setString("春…");
    if (sInfo.equals("4")) alt.setString("为什");
    if (sInfo.equals("5")) alt.setString("昨");
        alt.setTitle("您正在阅读短信" + sInfo); // 设置标题
    }
    else{
        alt.setString("很抱歉,暂时还没有您选择的项目,请重新输入!");
        alt.setTitle("错误警告"); // 设置标题
    }
        alt.setType(AlertType.INFO); // 设置为普通阅读信息框
        alt.setTimeout(Alert.FOREVER); // 信息窗口在按下DONE键后进入下一页面
        Display.getDisplay(this).setCurrent(alt, textbox); // 显示信息窗口
    }
    }
}
搜索更多相关的解决方案: import  javax  microedition  lcdui  手机  

----------------解决方案--------------------------------------------------------
检查你的模拟器是用的什么JDK安装的,有时候你的程序开发时用的JDK版本和模拟器的JDK版不一致,会导致这个问题的发生。例如:我的电脑上有两个版本的JDK,一个1.4.2,一个1.5.0_11,在Eclipse中开发时Eclipse默认是使用JDK1.5,但程序使用的JDK是1.4,这样就会出现楼主说的问题,只要把两个JDK版调一致就可以了
----------------解决方案--------------------------------------------------------
还有,不要出现中文路径名
----------------解决方案--------------------------------------------------------
很多东西是多余的,也是错误的
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.TextBox;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

public class MMS extends MIDlet implements CommandListener {
     private TextBox textbox; // TextBox 显示一条消息
     private Display disp; // 引用MIDlet的Display 对象
     private Command cmdExit; // 设定按钮用于退出MIDlet
     private Command cmdOK; // 确定按钮
     private Alert alt; // 信息提示对象

    public MMS() {
        disp = Display.getDisplay(this); // 获得当前MIDlet的Display对象
        cmdExit = new Command("退出", Command.SCREEN, 1); // 新建两个控制按钮
        cmdOK = new Command("阅读", Command.OK, 1);
        textbox = new TextBox("请输入待阅项目序号:", "", 40, 0); // 新建文本框
        textbox.addCommand(cmdExit); // 添加控制按钮
        textbox.addCommand(cmdOK);
        textbox.setCommandListener(this); // 开始侦听命令
    }

    protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
        // TODO Auto-generated method stub

    }

    protected void pauseApp() {
        // TODO Auto-generated method stub

    }

    protected void startApp() throws MIDletStateChangeException {
        alt = new Alert("整蛊专家 V1.0");// 开始运行时显示版权信息,新建信息框对象
        // 设置显示字符串
        alt.setString("==== 整蛊专家 V1.0 ====尹晓飞2007年作版权所有(C) 2007-2007");
        alt.setType(AlertType.INFO); // 设置为普通阅读信息框
        alt.setTimeout(Alert.FOREVER); // 信息窗口在按下DONE键后才能进入下一页面
        disp.setCurrent(alt, textbox); // 显示信息窗口

    }
   
    public void commandAction(Command arg0, Displayable arg1) {
    if (arg0 == cmdExit) { // 按下退出键时停止运行
       try {
        destroyApp(false);
    } catch (MIDletStateChangeException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
       notifyDestroyed();
    }
    if (arg0 == cmdOK) { // 按下阅读键后阅读对应信息
        TextBox textbox = (TextBox)arg1; // 得到用户输入的内容
        String sInfo = textbox.getString();
    if (sInfo.equals("1") || sInfo.equals("2") || sInfo.equals("3") || sInfo.equals("4") || sInfo.equals("5")) { // 显示项目1的内容
    // 根据所选项目选择要显示的内容
    if (sInfo.equals("1")) alt.setString("一兄");
    if (sInfo.equals("2")) alt.setString("猪");
    if (sInfo.equals("3")) alt.setString("春…");
    if (sInfo.equals("4")) alt.setString("为什");
    if (sInfo.equals("5")) alt.setString("昨");
        alt.setTitle("您正在阅读短信" + sInfo); // 设置标题
    }
    else{
        alt.setString("很抱歉,暂时还没有您选择的项目,请重新输入!");
        alt.setTitle("错误警告"); // 设置标题
    }
        alt.setType(AlertType.INFO); // 设置为普通阅读信息框
        alt.setTimeout(Alert.FOREVER); // 信息窗口在按下DONE键后进入下一页面
        Display.getDisplay(this).setCurrent(alt, textbox); // 显示信息窗口
    }
    }
}
----------------解决方案--------------------------------------------------------