2012-01-05 36 views
2

1.Problem上模擬器:
我在發起第一次我的MIDlet程序是存儲一些數據,然後我重新啓動它在第二次是讀取存儲的數據。前兩種情況沒有任何例外,它運行良好。與RMS的問題存儲持久性數據

但是我重新啓動它第二次以同樣的方式然後它給出異常:「未捕獲的異常java/lang/NumberFormatException:」它只處理字符和總數據少於64 kb。

2.關於真實設備的問題:
RMS根本不起作用。我不知道是否需要給予手機許可(諾基亞n95)。

謝謝。


在應用程序,它只是根據所選國家存儲慈善公司成rms。所以如果一個國家已經被選中,它必須跳過國家列表,然後在每次重啓時顯示公司列表。 在下面的代碼中,rms_Check()方法是檢查數據以打開國家或公司列表框架。

public class X { 

private static RecordStore rs =null; 
private static Vector rms_Vector = new Vector(); 
static final String REC_STORE ="db_1"; 

public X() { 

} 

public void openRecStore(){ 
    try { 
     rs = RecordStore.openRecordStore(REC_STORE, true); 
     System.out.println("open record store"); 
    } catch (Exception e) 
    { 
     db(e.toString()+" in openRecStore"); 
    } 
} 

public void closeRecStore(){ 
    try { 
     rs.closeRecordStore(); 
    } catch (Exception e) { 
     db(e.toString()+" in closeRecStore"); 
    } 
} 

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

public void writeRecord(String str) throws UnsupportedEncodingException 
{ 
    byte[] rec = str.getBytes("UTF-8"); 

    try { 
     rs.addRecord(rec, 0, rec.length); 
     System.out.println("write record store"); 
    } catch (Exception e) { 
     db(e.toString()+" in writeRecord"); 
    } 
} 

public void readRecord() 
{ 
    try { 
     // Intentionally it is too small to test code 
     byte[] m_enc = new byte[5]; 
     byte[] recData = new String(m_enc).getBytes("UTF-8"); 
     int len; 

     for(int i =1; i<= rs.getNumRecords(); i++){ 
     if(rs.getRecordSize(i)> recData.length) 
      recData = new byte[rs.getRecordSize(i)]; 

     len = rs.getRecord(i, recData, 0); 
     System.out.println("Record #"+i+":"+new String(recData, 0, len)); 
     System.out.println("------------------------"); 
     rms_Vector.addElement(new String(recData, 0, len)); 
     } 
    } catch (Exception e) { 
     db(e.toString() +" in readStore"); 
    } 
} 

private void db(String str) 
{ 
System.out.println("Msg:"+str); 
} 
public Vector rms_Array(){ 

    return this.rms_Vector; 
} 

public boolean rms_Check(){ 
if(this.rms_Vector.size()>0){ 
    System.out.print("rms_check: true"); 
    // if true it will display company list every time 
return true; 
}else{ 
    System.out.print("rms_check: false"); 
    //if false it will display country list then company list 
return false; 
} 
} 

} 
+0

你能分享有問題的代碼嗎?最好在[SSCCE](http://www.sscce.org/)表格​​中。至於權限,常規RMS操作不太可能需要權限 – gnat 2012-01-05 20:36:04

+0

請發佈代碼,以便我們瞭解到底是什麼問題? – 2012-01-06 06:03:49

+2

我分享了代碼的RMS部分並編輯了我的問題,我希望現在可以更容易理解。謝謝。 – 2012-01-06 10:41:52

回答

1

使用此

private RecordStore rs = null; // Record store 
public String REC_STORE = "RSM name"; // Name of record store 
public int record_max=0; 

public void openRecStore(){ 
try{ 
rs = RecordStore.openRecordStore(REC_STORE, true); 
}catch (Exception e){} 
} 

public void closeRecStore(){ 
try{ 
rs.closeRecordStore(); 
}catch (Exception e){} 
} 

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

public void writeRecord(String str){ 
byte[] rec = str.getBytes(); 

try{ 
rs.addRecord(rec, 0, rec.length); 
}catch (Exception e){} 
} 

    public void readRecords(){ 
    try{ 
    byte[] recData = new byte[5]; 
    int len; 
    record_max=rs.getNumRecords(); 
    for(int i = 1; i <= record_max; i++){ 

     if(rs.getRecordSize(i) > recData.length){ 
     recData = new byte[rs.getRecordSize(i)]; 

     } 
     len = rs.getRecord(i, recData, 0); 
     file_name[i]=new String(recData, 0, len); 

     } 
    }catch (Exception e){} 
    } 

你有FILE_NAME []數組保存數據

負載肌動蛋白commad使用:

  openRecStore(); 
      readRecords(); 
      for(int j=1;j<=record_max;j++) { 
      System.out.println("Record " + j + " : " + file_name[j]); 
       } 
      closeRecStore(); 

並保存此:

  openRecStore(); 
      writeRecord(textField.getString()); 
      closeRecStore();