dcsimg
Anybody know why this won't work ???
2 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Jack_Cowhey
Posted On:   Friday, March 3, 2006 08:07 AM

I'm new to J2ME ... I don't know why my code won't work ... It's just for saving the contents of a textfield to a record store ... It compiles with no errors but then the emulator comes up blank and crashes ... Any help would be greatly appreciated .. Thanks, Jack. import javax.microedition.lcdui.Command; import javax.microedition.lcdui.CommandListener; import javax.microedition.lcdui.Displayable; import javax.microedition.lcdui.Form; import javax.microedition.lcdui.TextField; import javax.microedition.midlet.MIDlet; import javax.microedition.midlet.MIDletStateChangeException; import javax.microedition.rms.RecordStore; public class SaveTest extends MIDlet    More>>

I'm new to J2ME ... I don't know why my code won't work ... It's just for saving the contents of a textfield to a record store ... It compiles with no errors but then the emulator comes up blank and crashes ... Any help would be greatly appreciated ..
Thanks,
Jack.

			
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
import javax.microedition.rms.RecordStore;

public class SaveTest extends MIDlet implements CommandListener {

private String name[];
private String score[];
private Command exitCommand = new Command("Exit", Command.EXIT, 0);
private Command saveCommand = new Command("Save", Command.SCREEN, 1);
private Command loadCommand = new Command("Load", Command.SCREEN, 2);
private Form test;
SaveTest testrecord = new SaveTest();
RecordStore rs = null;
private TextField Textfieldname = new TextField("Name", "", 15, TextField.ANY);
private TextField Textfieldscore = new TextField("Score", "", 15, TextField.NUMERIC);

protected void startApp() throws MIDletStateChangeException {

//Form test = new Form(playerName);
test.append(Textfieldname);
test.append(Textfieldscore);
test.addCommand(exitCommand);
test.addCommand(saveCommand);
test.addCommand(loadCommand);
score[0] = Textfieldscore.getString();
name[0] = Textfieldname.getLabel();
}
protected void pauseApp() {
// TODO Auto-generated method stub

}
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
// TODO Auto-generated method stub

}
public void commandAction(Command c, Displayable arg1) {
if (c == exitCommand) {
destroyApp(false);
notifyDestroyed();
}

if (c == saveCommand) {
save();

}

if (c == loadCommand) {
load();
}
// TODO Auto-generated method stub

}
private void load() {

// Open the record store
RecordStore rs = null;
try {
rs = RecordStore.openRecordStore("HiScores", false);
}
catch (Exception e) {
}

if (rs != null) {
// Read the records
try {
int len;
byte[] recordData = new byte[8];

for (int i = 1; i <= rs.getNumRecords(); i++) {
// Re-allocate record holder if necessary
if (rs.getRecordSize(i) > recordData.length)
recordData = new byte[rs.getRecordSize(i)];

// Read the score and store it in the score array
len = rs.getRecord(i, recordData, 0);
score[i - 1] = ((new String(recordData, 0, len)));
}
}
catch (Exception e) {
System.err.println("Failed reading hi scores!");
}

// Close the record store
try {
rs.closeRecordStore();
}
catch (Exception e) {
System.err.println("Failed closing hi score record store!");
}
}
else {
// The record store doesn't exist, so initialize the scores to 0
for (int i = 0; i < 5; i++)
score[i] = null;
}
}


public void save() {

{
// Delete the previous scores record store

// Create the new scores record store
RecordStore rs = null;
try {
rs = RecordStore.openRecordStore("HiScores", true);
}
catch (Exception e) {
System.err.println("Failed creating hi score record store!");
}

// Write the scores
for (int i = 0; i < 5; i++) {
// Format each score for writing
byte[] recordData = (score[i]).getBytes();

try {
// Write the score as a record
rs.addRecord(recordData, 0, recordData.length);
}
catch (Exception e) {
System.err.println("Failed writing hi scores!");
}
}

// Close the record store
try {
rs.closeRecordStore();
}
catch (Exception e) {
System.err.println("Failed closing hi score record store!");
}
}

}
}
   <<Less

Re: Anybody know why this won't work ???

Posted By:   Anonymous  
Posted On:   Tuesday, March 14, 2006 06:35 AM

Hi try this...




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.TextField;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
import javax.microedition.rms.RecordStore;

public class SaveTest extends MIDlet implements CommandListener {

private String name[];
private String score[];
private Command exitCommand = new Command("Exit", Command.EXIT, 0);
private Command saveCommand = new Command("Save", Command.SCREEN, 1);
private Command loadCommand = new Command("Load", Command.SCREEN, 2);
private Form test;
// SaveTest testrecord = new SaveTest();
RecordStore rs = null;
private TextField Textfieldname = new TextField("Name", "", 15, TextField.ANY);
private TextField Textfieldscore = new TextField("Score", "", 15, TextField.NUMERIC);
Display display=null;

protected void startApp() {

display=Display.getDisplay((MIDlet) this);
Form test = new Form("playerName");
test.append(Textfieldname);
test.append(Textfieldscore);
test.addCommand(exitCommand);
test.addCommand(saveCommand);
test.addCommand(loadCommand);
score=new String[2];
name=new String[2];

score[0] = Textfieldscore.getString();
name[0] = Textfieldname.getLabel();
display.setCurrent(test);
}

protected void pauseApp() {
// TODO Auto-generated method stub

}
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
// TODO Auto-generated method stub

}
/* (non-Javadoc)
* @see javax.microedition.lcdui.CommandListener#commandAction(javax.microedition.lcdui.Command, javax.microedition.lcdui.Displayable)
*/
public void commandAction(Command c, Displayable arg1) {
if (c == exitCommand) {
try {
destroyApp(false);
} catch (MIDletStateChangeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
notifyDestroyed();
}

if (c == saveCommand) {
save();
}

if (c == loadCommand) {
load();
}
// TODO Auto-generated method stub

}
private void load() {

// Open the record store
RecordStore rs = null;
try {
rs = RecordStore.openRecordStore("HiScores", false);
}
catch (Exception e) {
}

if (rs != null) {
// Read the records
try {
int len;
byte[] recordData = new byte[8];

for (int i = 1; i <= rs.getNumRecords(); i++) {
// Re-allocate record holder if necessary
if (rs.getRecordSize(i) > recordData.length)
recordData = new byte[rs.getRecordSize(i)];

// Read the score and store it in the score array
len = rs.getRecord(i, recordData, 0);
score[i - 1] = ((new String(recordData, 0, len)));
}
}
catch (Exception e) {
System.err.println("Failed reading hi scores!");
}

// Close the record store
try {
rs.closeRecordStore();
}
catch (Exception e) {
System.err.println("Failed closing hi score record store!");
}
}
else {
// The record store doesn't exist, so initialize the scores to 0
for (int i = 0; i < 5; i++)
score[i] = null;
}
}


public void save() {

{
// Delete the previous scores record store

// Create the new scores record store
RecordStore rs = null;
try {
rs = RecordStore.openRecordStore("HiScores", true);
}
catch (Exception e) {
System.err.println("Failed creating hi score record store!");
}

// Write the scores
for (int i = 0; i < 5; i++) {
// Format each score for writing
byte[] recordData = (score[i]).getBytes();

try {
// Write the score as a record
rs.addRecord(recordData, 0, recordData.length);
}
catch (Exception e) {
System.err.println("Failed writing hi scores!");
}
}

// Close the record store
try {
rs.closeRecordStore();
}
catch (Exception e) {
System.err.println("Failed closing hi score record store!");
}
}

}
}



- kamal

Re: Anybody know why this won't work ???

Posted By:   Anonymous  
Posted On:   Friday, March 3, 2006 12:25 PM

Remove the line SaveTest testrecord = new SaveTest();

Only Object of the Midlet should not be created as part of program. This Oject is intialized by the Application Manager of the Phone or Emulator.

Regards,
Nagendra
C.T.O
TejaSoft.com
About | Sitemap | Contact