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

请高手给看一下,为什么小弟我的rs总是存不下来呢

2012-01-15 
请高手给看一下,为什么我的rs总是存不下来呢?/**创建日期2007-5-17**TODO要更改此生成的文件的模板,请转至

请高手给看一下,为什么我的rs总是存不下来呢?
/*
  *   创建日期   2007-5-17
  *
  *   TODO   要更改此生成的文件的模板,请转至
  *   窗口   -   首选项   -   Java   -   代码样式   -   代码模板
  */
package   example;

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.Form;
import   javax.microedition.lcdui.StringItem;
import   javax.microedition.lcdui.TextBox;
import   javax.microedition.lcdui.TextField;
import   javax.microedition.midlet.MIDlet;
import   javax.microedition.midlet.MIDletStateChangeException;
import   javax.microedition.rms.RecordStore;
import   javax.microedition.rms.RecordStoreNotFoundException;

/**
  *   @author   Administrator
  *
  *   TODO   要更改此生成的类型注释的模板,请转至
  *   窗口   -   首选项   -   Java   -   代码样式   -   代码模板
  */
public   class   Rr   extends   MIDlet   implements   CommandListener   {
       
        private   Command   exitCommand;
        private   Command   setCommand;
        private   Display   display;
        private   Form   aForm;
        private   TextField   tf;
        //private   StringItem   si;
        private   Alert   al;
        private   String   ss;
       
        private   int   a   =   333;
       
        private   Command   backCommand;
        private   int   start   =   0;
       
        String   s[];
       
        public   Rr()   {
                super();

                display   =   Display.getDisplay(this);
                exitCommand   =   new   Command( "离开 ",Command.SCREEN,1);
                aForm   =   new   Form( "dsdf ");
                tf   =   new   TextField(null,null,256,TextField.ANY);
                //si   =   new   StringItem(null, "请输入: ");
                setCommand   =   new   Command( "保存 ",Command.SCREEN,1);
                //aForm.append(si);
                aForm.append(tf);
                aForm.addCommand(setCommand);
                aForm.addCommand(exitCommand);
                aForm.setCommandListener(this);


               
               
                TextBox   aTextBox;
                backCommand   =   new   Command( "返回 ",Command.BACK,1);
               
               
                //   TODO   自动生成构造函数存根
        }

        protected   void   startApp()   throws   MIDletStateChangeException   {
                //   TODO   自动生成方法存根
               
                //TextBox   aTextBox   =   new   TextBox( "主画面 ",null,256,TextField.ANY);

               
                display.setCurrent(aForm);
             
               
        }

        protected   void   pauseApp()   {
                //   TODO   自动生成方法存根

        }

        protected   void   destroyApp(boolean   arg0)   throws   MIDletStateChangeException   {
                //   TODO   自动生成方法存根

        }

        public   boolean   existing(String   recordStoreName)
        {
                boolean   existingOrNot   =   true;
                RecordStore   rs   =   null;
                if(recordStoreName.length()   >   32)
                        return   false;
                try
                {
                        rs   =   RecordStore.openRecordStore(recordStoreName,false);
                }
                catch(RecordStoreNotFoundException   e)
                {
                        existingOrNot   =   false;
                }
                catch(Exception   e)
                {}
                finally
                {
                        try
                        {
                                rs.closeRecordStore();


                        }
                        catch(Exception   e)
                        {}
                }
               
                return   existingOrNot;
        }
       
       
        public   void   commandAction(Command   arg0,   Displayable   arg1)   {
                //   TODO   自动生成方法存根
               
                if(arg0   ==   setCommand)
                {
                       
                        saveRecord();
                        /*
                        ss   =   tf.getString();
                        al   =   new   Alert( "显示 ",ss,null,AlertType.CONFIRMATION);
                        al.setTimeout(Alert.FOREVER);
                        display.setCurrent(al);
                        */
                       
                }
                //notifyDestroyed();
               
                if(arg0   ==   exitCommand)
                {
                        getRec();
                }
               
                if(arg0   ==   backCommand)
                        display.setCurrent(aForm);
               

        }
       
       
        public   void   saveRecord()
        {
                RecordStore   rs   =   null;
                byte[]   buffer   =   null;
                boolean   existingOrNot   =   false;
                boolean   OK   =   true;
               


                existingOrNot   =   existing( "memo ");
                if(existingOrNot)
                {
                        try
                        {
                                rs   =   RecordStore.openRecordStore( "memo ",false);
                        }
                        catch(Exception   e)
                        {
                                OK   =   false;
                        }
                        finally
                        {
                                if(OK)
//                                         tf.setString( "memo已存在并且已打开 ");
                                        a   =   111;
                                else
//                                         tf.setString( "memo已存在但无法打开 ");
                                        a   =   222;
                        }
                }



[解决办法]
rms是缓冲处理的,你只有正常的退出MIDlet,rms才会保存到硬盘上

注意在你退出MIDlet的代码处,添加

destroyApp(false);
notifyDestroyed();
[解决办法]
可能是模拟器的问题,最好在真机上测试.

saltedfish
[解决办法]
try
{
rs = RecordStore.openRecordStore(recordStoreName,false);
~~~~~`这里应该写true吧,不然这个rs 不会被创建,你怎么存数据呢?
}
catch(RecordStoreNotFoundException e)
{
existingOrNot = false;
}

热点排行