RMS 存储值在 J2ME 仿真器中不是永久可用的

Posted

技术标签:

【中文标题】RMS 存储值在 J2ME 仿真器中不是永久可用的【英文标题】:RMS stored values are not permanently available in J2ME emulator 【发布时间】:2012-05-20 10:50:34 【问题描述】:

我有代码在 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

【问题讨论】:

请上传您的代码。 您是 J2ME/编程新手吗? 是的,我已经提到过。 请任何人帮助我找到 NetBeans 中的 storage_root 选项,或者在我的手机中存储和检索数据的任何替代方法。 【参考方案1】:

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

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

【讨论】:

我该怎么做,因为我是 J2ME 的新手。提前致谢!! 我现在没有安装 WTK,右键单击 netbeans 中的项目,然后应该有一些设置叫做管理模拟器检查应该有一个选项 storage_root,你应该看到类似的东西this 我找到了manage Emulator的选项,但是没有找到像storage_root这样的选项 检查图像并尝试找到它 我刚刚上传了截图,点击Manage Emulator

以上是关于RMS 存储值在 J2ME 仿真器中不是永久可用的的主要内容,如果未能解决你的问题,请参考以下文章

J2ME RMS - 打开/关闭记录存储的最佳实践?

在 J2ME 中存储数据的文件

J2ME 中的数据库

为啥我不能将UTF写入RMS?

如果我关闭设备,使用 RMS 保留的数据会发生啥情况

java错误(j2me)