gpt4 book ai didi

java - RMS 存储的值在 J2ME 模拟器中不是永久可用的

转载 作者:行者123 更新时间:2023-12-04 05:46:05 25 4
gpt4 key购买 nike

我有在 J2ME 中使用 RMS 存储值的代码。在模拟器上运行良好。所以,我的第一个问题是

  • 当我重新启动模拟器时,所有存储的值都被删除。
  • 存储的值显示在模拟器中,但没有显示在我正在测试我的应用程序的移动设备中。

  • 我正在使用 NetBeans 来开发我的 J2ME 应用程序。

    ===更新===
    /*
    * To change this template, choose Tools | Templates
    * and open the template in the editor.
    */

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

    public class TryNew extends MIDlet implements CommandListener, ItemCommandListener {

    private RecordStore record;
    private StringItem registered;
    static final String REC_STORE = "SORT";
    //Button existUser;
    Display display = null;
    private Ticker ticker;
    Form form = null;
    Form form1 = null;
    TextField tb, tb1, tb2, tb3;
    ChoiceGroup operator = null;
    String str = null;
    Command backCommand = new Command("Back", Command.BACK, 0);
    Command loginCommand = new Command("Login", Command.OK, 2);
    Command saveCommand = new Command("Save New", Command.OK, 1);
    Command sendCommand = new Command("Send", Command.OK, 2);
    Command selectCommand = new Command("Select", Command.OK, 0);
    Command exitCommand = new Command("Exit", Command.STOP, 3);
    private ValidateLogin ValidateLogin;

    public TryNew() {
    }

    public void startApp() throws MIDletStateChangeException {
    display = Display.getDisplay(this);
    form = new Form("Login");
    registered = new StringItem("", "Registered ?", StringItem.BUTTON);
    form1 = new Form("Home");
    tb = new TextField("Login Id: ", "", 10, TextField.PHONENUMBER);//TextField.PHONENUMBER
    tb1 = new TextField("Password: ", "", 30, TextField.PASSWORD);
    operator = new ChoiceGroup("Select Website", Choice.POPUP, new String[]{"Game", "Joke", "SMS"}, null);
    form.append(tb);
    form.append(tb1);
    form.append(operator);
    form.append(registered);
    registered.setDefaultCommand(selectCommand);
    registered.setItemCommandListener(this);
    form.addCommand(saveCommand);
    ticker = new Ticker("Welcome Screen");
    form.addCommand(loginCommand);
    form.addCommand(selectCommand);
    form.addCommand(exitCommand);
    // existUser = new StringItem(null, "Registered ?");
    // form.append(existUser);
    form.setCommandListener(this);
    form1.addCommand(exitCommand);
    form1.addCommand(sendCommand);
    form1.setCommandListener(this);
    form.setTicker(ticker);
    display.setCurrent(form);
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
    }

    void showMessage(String message, Displayable displayable) {
    Alert alert = new Alert("");
    alert.setTitle("Error");
    alert.setString(message);
    alert.setType(AlertType.ERROR);
    alert.setTimeout(5000);
    display.setCurrent(alert, displayable);
    }

    public void commandAction(Command c, Displayable d) {
    if (c == exitCommand) {
    destroyApp(true);
    notifyDestroyed();
    } else if (c == backCommand) {
    display.setCurrent(form);
    } else if (c == loginCommand) {
    ValidateLogin = new ValidateLogin(this);
    ValidateLogin.start();
    ValidateLogin.validateLogin(tb.getString(), tb1.getString(), operator.getString(operator.getSelectedIndex()));
    } else if (c == saveCommand) {
    openRecord();
    writeRecord(tb.getString(), tb1.getString(), operator.getString(operator.getSelectedIndex()));
    closeRecord();
    showAlert("Login Credential Saved Successfully !!");
    }
    }

    ////==============================================================================/////
    /// Record Management
    public void openRecord() {
    try {
    record = RecordStore.openRecordStore(REC_STORE, true);
    } catch (Exception e) {
    db(e.toString());
    }
    }

    public void closeRecord() {
    try {
    record.closeRecordStore();
    } catch (Exception e) {
    db(e.toString());
    }
    }

    public void deleteRecord() {
    if (RecordStore.listRecordStores() != null) {
    try {
    RecordStore.deleteRecordStore(REC_STORE);
    } catch (Exception e) {
    db(e.toString());
    }
    }
    }

    public void writeRecord(String login_id, String pwd, String operator_name) {
    String credential = login_id + "," + pwd + "," + operator_name;
    byte[] rec = credential.getBytes();
    try {
    if (login_id.length() > 10 || login_id.length() < 10) {
    showAlert("Please Enter valid Login Id");
    } else if (pwd.length() < 1) {
    showAlert("Please Password !!");
    } else {
    record.addRecord(rec, 0, rec.length);
    }

    } catch (Exception e) {
    db(e.toString());
    }
    }

    private void showAlert(String err) {
    Alert a = new Alert("");
    a.setString(err);
    a.setTimeout(Alert.FOREVER);
    display.setCurrent(a);
    }

    public void readRecord() {
    try {
    if (record.getNumRecords() > 0) {
    Comparator comp = new Comparator();
    RecordEnumeration re = record.enumerateRecords(null, comp, false);

    while (re.hasNextElement()) {
    String str = new String(re.nextRecord());
    showAlert(str);
    }
    }
    } catch (Exception e) {
    db(e.toString());
    }
    }

    private void db(String error) {
    System.err.println("Exception: " + error);
    }

    public void commandAction(Command c, Item item) {
    if (c == selectCommand && item == registered) {
    openRecord();
    readRecord();
    closeRecord();
    }
    }

    class Comparator implements RecordComparator {

    public int compare(byte[] rec1, byte[] rec2) {
    String str1 = new String(rec1);
    String str2 = new String(rec2);
    int result = str1.compareTo(str2);
    if (result == 0) {
    return RecordComparator.EQUIVALENT;
    } else if (result < 0) {
    return RecordComparator.PRECEDES;
    } else {
    return RecordComparator.FOLLOWS;
    }
    }
    }

    class ValidateLogin implements Runnable {

    TryNew midlet;
    private Display display;
    String login_id;
    String pwd;
    String operator_name;

    public ValidateLogin(TryNew midlet) {
    this.midlet = midlet;
    display = Display.getDisplay(midlet);
    }

    public void start() {
    Thread t = new Thread(this);
    t.start();
    }

    public void run() {
    if (login_id.length() > 10 || login_id.length() < 10) {
    showAlert("Please Enter valid Login Id");
    } else if (pwd.length() < 1) {
    showAlert("Please Password !!");
    } else {
    showHome();
    }
    }
    /* This method takes input from user like text and pass
    to servlet */

    public void validateLogin(String login_id, String pwd, String operator_name) {
    this.login_id = login_id;
    this.pwd = pwd;
    this.operator_name = operator_name;
    }

    /* Display Error On screen*/
    private void showAlert(String err) {
    Alert a = new Alert("");
    a.setString(err);
    a.setTimeout(Alert.FOREVER);
    display.setCurrent(a);
    }

    private void showHome() {
    tb2 = new TextField("To: ", "", 30, TextField.PHONENUMBER);
    tb3 = new TextField("Message: ", "", 300, TextField.ANY);
    form1.append(tb2);
    form1.append(tb3);
    form1.addCommand(loginCommand);
    //display.setCurrent(tb3);
    display.setCurrent(form1);

    }
    };
    }

    这就是我得到的,当我点击 Manage Emulator

    Manage Emulator

    最佳答案

    您需要修复 storage_root您在 WTK 中的应用程序,

    每当您启动 enulator 时,它都会引用相同的 storage_root,默认情况下,它会为每个 session 创建不同的数据文件

    关于java - RMS 存储的值在 J2ME 模拟器中不是永久可用的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10672771/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com