2014-03-31 8 views
0

RMS를 사용하여 Jave ME에 애플리케이션을 작성하려고합니다. 응용 프로그램은 택배 고객에 대한 정보를 저장합니다.RMS를 사용하는 JaveME의 애플리케이션

import javax.microedition.midlet.*; 
import javax.microedition.lcdui.*; 
import javax.microedition.rms.*; 
import java.io.ByteArrayOutputStream; 
import java.io.ByteArrayInputStream; 
import java.io.DataOutputStream; 
import java.io.DataInputStream; 

public class ClientsApp extends MIDlet implements CommandListener { 
// controls 
private Display screen = null; 
private List menu = null; 
private Form addClientForm = null; 
private Form showClientForm = null; 
private RecordStore clients; 
private ByteArrayOutputStream stream = null; 
private DataOutputStream out = null; 
private byte[] dates; 

TextField name = null; 
TextField surname = null; 
TextField email = null; 
TextField phone = null; 
DateField date = null; 
TextField price = null; 
TextField description = null; 


// comands 
private final Command backCommand; 
private final Command mainMenuCommand; 
private final Command exitCommand; 
private final Command addClientCommand; 

public ClientsApp() { 
    // initializating controls and comands 
    menu = new List("Lista klientów", Choice.IMPLICIT); 
    backCommand = new Command("Cofnij", Command.BACK, 0); 
    mainMenuCommand = new Command("Main", Command.SCREEN, 1); 
    exitCommand = new Command("Koniec", Command.EXIT, 2); 
    addClientCommand = new Command("Zapisz", Command.OK, 3);  
    stream = new ByteArrayOutputStream(); 
    out = new DataOutputStream(stream); 

    menu.append("Dodaj klienta", null); 
    menu.append("Przegladaj klientow", null); 
    menu.append("Usun klienta", null); 
} 

protected void destroyApp(boolean arg0) throws MIDletStateChangeException { 

} 

protected void pauseApp() { 

} 

protected void startApp() throws MIDletStateChangeException { 
    screen = Display.getDisplay(this); 
    screen.setCurrent(menu); 

    try { 
     clients = RecordStore.openRecordStore("clients", false, RecordStore.AUTHMODE_PRIVATE, false); 
    } 
    catch(RecordStoreException exc) { 
    } 

    menu.addCommand(exitCommand); 
    menu.setCommandListener(this); 
} 

public void commandAction(Command cmd, Displayable dsp) { 
    if(cmd.getCommandType() == Command.EXIT) { 
     try{ 
      destroyApp(false); 
      notifyDestroyed(); 
     } 
     catch(Exception exc) { 
      exc.printStackTrace();    
     } 
    } 
    else if(cmd.getCommandType() == Command.BACK) { 
     screen.setCurrent(menu); 
    } 
    else if(cmd.getCommandType() == Command.OK) { 
     try { 
      out.writeUTF(name.getString()); 
      out.writeUTF(surname.getString()); 
      out.writeUTF(email.getString()); 
      out.writeUTF(phone.getString()); 
      out.writeUTF(date.getDate().toString()); 
      out.writeUTF(price.getString()); 
      out.writeUTF(description.getString()); 
      dates = stream.toByteArray(); 

      clients.addRecord(dates, 0, dates.length); 

      stream.close(); 
      out.close(); 
      clients.closeRecordStore(); 
     } 
     catch(Exception exc) { 

     } 
    } 
    else { 
     List option = (List) screen.getCurrent(); 

     switch(option.getSelectedIndex()) { 
     case 0 : { 
      addClients(); 
      break; 
     } 
     case 1 : { 
      showClients(); 
      break; 
     } 
     case 2 : { 
      deleteClients(); 
      break; 
     } 
     } 
    } 
} 

protected void addClients() { 
    addClientForm = new Form("Dodaj klienta"); 
    name = new TextField("Imię klienta", "", 10, TextField.ANY); 
    surname = new TextField("Nazwisko klienta", "", 15, TextField.ANY); 
    email = new TextField("Email klienta", "", 20, TextField.EMAILADDR); 
    phone = new TextField("Numer telefonu", "", 12, TextField.PHONENUMBER); 
    date = new DateField("Data dostarczenia", DateField.DATE); 
    price = new TextField("Do zapłaty", "", 6, TextField.NUMERIC); 
    description = new TextField("Uwagi", "", 50, TextField.ANY); 

    addClientForm.append(name); 
    addClientForm.append(surname); 
    addClientForm.append(email); 
    addClientForm.append(phone); 
    addClientForm.append(date); 
    addClientForm.append(price); 
    addClientForm.append(description); 
    screen.setCurrent(addClientForm); 

    addClientForm.addCommand(backCommand); 
    addClientForm.addCommand(addClientCommand); 
    addClientForm.setCommandListener(this); 
} 

protected void showClients() { 
    TextBox info = new TextBox("Klienci", null, 100, 0); 

    RecordEnumeration iterator = null; 
    String str = null; 
    byte[] temp = null; 

    try { 
     iterator = clients.enumerateRecords(null, null, false); 

     while(iterator.hasNextElement()) { 
      temp = iterator.nextRecord(); 
     } 

     for(int i = 0; i < temp.length; i++) { 
      str += (char) temp[i]; 
     } 
     System.out.println(str); 
     clients.closeRecordStore(); 
    } 
    catch(Exception exc) { 

    }  

    info.setString(str); 
    screen.setCurrent(info); 
} 
} 

RecordStore의 정보 읽기/쓰기가 작동하지 않습니다. 나는 예외를 던지지 않습니다. 누군가 나를 도울 수 있을까요?

추자. 저의 언어는 유감입니다.

답변

0

정말 예외가 없습니까? catch 블록이 비어 있습니다 ... 몇 가지 문제가 나타납니다 : createIfNecessary (두 번째 매개 변수)가 true로 설정된 레코드 저장소를 열지 않아야합니까? ShowClients 메서드에서 DataInputStream을 사용하여 레코드의 항목 (바이트 배열 'temp')을 읽어야합니다. 임시 루프가 이상합니다. 그리고 상점이 비었을 때 NPE를 피하기위한 'temp'값이 없는지 확인합니다. On OK 명령과 ShowClients에서도 저장소가 닫혀 있으므로 다음에 RecordStoreNotOpenException과 함께 실패합니다.

나는 또한 좋은 연습에 불과하다 (DataOutputStrea /있는 ByteArrayOutputStream)이 경우에 있지만, stream.toByteArray()를 호출하기 전에 스트림을 '밖으로'플러싱이라고 생각

..