MIDlet navigation
Hi all
Forgive me if this has been asked before.
It is a basic question about MIDlet navigation.
Supossing I have a MIDlet that displays a form which is basically a menu.
Then each menu option the user selects is another displayable object. EG. Form or Canvas etc.
The current way I have coded it is within my commandAction or itemAction I do
Code:
if ( action == someAction )
{
instance.display.setCurrent(new MyForm() );
}
elseif ( action == someAction )
{
instance.display.setCurrent(new MyOtherForm() );
}
is this the right way to perform MIDlet navigation? Or should it be a a suite of MIDlets?
Basically it seems as if the above is wrong? because when I run this on the emulator Nokia or WTK it works fine but running on a target device (In this case Nokia 6600 ) it errors? which seems to be a memory error?
I can post the Page it is supposed to be display if anyone wants to see that?
Any ideas?
Regards
eggsy
[1316 byte] By [
jimbobegga] at [2007-11-26 21:57:29]

# 4
This is a sample page that wont display:
package com.paje.app.menu;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import javax.microedition.lcdui.ChoiceGroup;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Item;
import javax.microedition.lcdui.ItemCommandListener;
import javax.microedition.lcdui.StringItem;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;
import com.paje.app.Pajelet;
import com.paje.app.bluetooth.BluetoothServer;
import com.paje.app.persistent.UserStatus;
public class ActivatePage extends Form implements CommandListener, ItemCommandListener
{
//Status statics
private static final int INVISIBLE = 0;
private static final int VISIBLE = 1;
private ChoiceGroup statusField;
private Item saveButton;
private RecordStore statusData;
private boolean openedRecordStore = false;
private static final Command SAVE_CMD = new Command("Save", Command.ITEM, 2);
private static final Command MENU_BACK_CMD = new Command("Back", Command.BACK, 0);
private static final String RECORD_STORE = "Visibility";
//Make us a Bluetooth Server?!
private BluetoothServer btServer;
public ActivatePage()
{
super( "Activate Matching");
addCommand(MENU_BACK_CMD);
setCommandListener(this);
openRecordStore();
UserStatus userStatus = loadStatus();
statusField = new ChoiceGroup("Status:", ChoiceGroup.EXCLUSIVE );
statusField.insert(INVISIBLE, "Invisible", null);
statusField.insert(VISIBLE, "Visible", null);
//Get the users stored status
//0 for not active 1 for active
int status = userStatus.getActive();
if ( status == 0 )
{
statusField.setSelectedIndex(0, true);
}
else
{
statusField.setSelectedIndex(1, true);
}
append( statusField );
saveButton = new StringItem("", "Save Changes", Item.BUTTON );
saveButton.setDefaultCommand(SAVE_CMD);
saveButton.setItemCommandListener(this);
append( saveButton );
btServer = new BluetoothServer();
}
public void openRecordStore()
{
try
{
statusData = RecordStore.openRecordStore(RECORD_STORE, true);
openedRecordStore = true;
}
catch(RecordStoreException e )
{
System.err.println( "Error opening record store" );
return;
}
}
public void closeRecordStore()
{
if ( openedRecordStore )
{
try
{
statusData.closeRecordStore();
openedRecordStore = false;
}
catch (RecordStoreException e)
{
}
}
}
public void deleteRecordStore()
{
closeRecordStore();
if (RecordStore.listRecordStores() != null)
{
try
{
RecordStore.deleteRecordStore(RECORD_STORE);
}
catch (RecordStoreException e)
{
System.err.println( "Could not delete the record store");
e.printStackTrace();
}
}
}
public void commandAction(Command c, Displayable d)
{
if ( c == MENU_BACK_CMD )
{
closeRecordStore();
Pajelet.display.setCurrent( new MainMenu());
}
}
public void commandAction(Command c, Item i)
{
if ( c == SAVE_CMD )
{
try
{
saveChanges();
if ( statusField.getSelectedIndex() == VISIBLE )
{
System.out.println( "Bluetooth Activation going online...");
btServer.startServer();
}
else
{
System.out.println( "Bluetooth Activation going offline....");
btServer.stopServer();
}
}
catch( Exception err )
{
System.err.println( "Exception thrown during Saving activation: \n" + err );
}
closeRecordStore();
String message = " Your activity has now been changed";
SavedChanges.showSavedConfirmation( message, Pajelet.display );
}
}
public UserStatus loadStatus()
{
UserStatus user = null;
try
{
byte [] data = statusData.getRecord(1);
DataInputStream is = new DataInputStream( new ByteArrayInputStream(data));
user = new UserStatus();
user.setActive(is.readInt());
is.close();
}
catch (Exception e )
{
//User has not saved changes and thus we just need to create one
user = new UserStatus();
}
return user;
}
public void saveChanges() throws Exception
{
deleteRecordStore();
if ( !openedRecordStore )
{
openRecordStore();
}
UserStatus userStatus = new UserStatus();
if ( statusField.getSelectedIndex() == 0 )
{
userStatus.setActive( 0 );
}
else
{
userStatus.setActive( 1 );
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream os = new DataOutputStream(baos);
os.writeInt(userStatus.getActive() );
os.close();
byte[] data = baos.toByteArray();
statusData.addRecord(data, 0, data.length);
}
Does there seem to be anything that the 6600 wouldnt support?
# 5
Hey
Are you creating seperate Midlet extended class?
If yes then plz show that code so that we can make sure that u are following proper Midlet life cycle and importing all important packages.
For your convenience I can give you an example how we do navigation in btw high level components on midlet:::
--
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
public class ABC extends MIDlet implements CommandListener, ItemStateListener {
public Display display;
protected Form form1, form2;
private Command commandForm2 = new Command("Next", Command.SELECT, 5);
public ABC(){
display = Display.getDisplay(this);
form1 = new Form("Form 1");
form2 = new Form("Form 2");
form1.addCommand(commandForm2);
form1.setCommandListener(this);
form1.setItemStateListener(this);
}
public void startApp() {
display.setCurrent(form1);
}
protected void pauseApp() {
}
protected void destroyApp(boolean unconditional) {
}
public void commandAction(Command command, Displayable displayable) {
if (command == commandForm2{
display.setCurrent(form2);
}
It is just a rough idea I hope it will help you.
If any more query then clear in next post with proper errors which it is showing when u run on different different phones.
Regards!
J2meFighter