Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Developer
Posts: 25
Registered: ‎01-29-2009
My Device: Curve 8330
Accepted Solution

How to push second screen?

Hi,

I am writing an application in which I am trying to push a second screen from a custom menu item in the first. The first screen is pushed succesfully and the custom menu items are populated from a bluetoothserialportlistener. Once I click on a particular menu item i would like to launch the second screen. I have used a dialog.alert to verify that the run() method of the menuitem is working but the second screen is not displayed.

 

Any suggestions are greatly appreciated!

 

The first screen is created  in main(String [] args) by

 

MatMain app =
new MatMain();

app.enterEventDispatcher();

 

private MatMain(){

pushScreen( new WelcomeScreen());

}

 

 

I have tried calling the second screen as follows:

 

 synchronized(UiApplication.getEventLock()){

getUiEngine().pushScreen(new MatOnline(_info));

}

 

i also tried:

 

//UiApplication.getUiApplication().pushScreen(new MatOnline(_info));

 

 

Thanks again!

 

 

 

Developer
Posts: 25
Registered: ‎01-29-2009
My Device: Curve 8330

Re: How to push second screen?

I also tried: 

UiApplication.getUiApplication().invokeLater(new Runnable(){

 

public void run()

{

UiApplication.getUiApplication().pushScreen(
new MatOnline(_info));

 

}});

 

 

which throws an uncaught exception: java.lang.NullPointerException

Developer
Posts: 984
Registered: ‎02-10-2009
My Device: Not Specified

Re: How to push second screen?

Try the following approach

 

 

private MenuItem testMenu = new MenuItem("Test Menu",110,100) { public void run() { UiApplication.getUiApplication().pushScreen(new MatOnline(_info)); } };

 

If this approach does not work then the problem must be in your  MatOnline Screen.

 

 

Regards

Bikas 

 

 

Developer
Posts: 25
Registered: ‎01-29-2009
My Device: Curve 8330

Re: How to push second screen?

Thanks for your suggestion.

 

I am starting to think you might be right about the MatOnline screen.

 

I am pushing it with

MatMain.getUiApplication().pushScreen(new MatOnline(_info));

and it is running the code in that class. (connects via bluetooth (DTR true) and pops up dialog alert)

But it doesnt display any of the objects i am adding to that screen... instead it is just showing my first WelcomeScreen

 

Is there something i am missing when adding buttons, labels and managers to the second screen?

 

 the MatOnline class extends MainScreen and I am declaring the objects by:

 

LabelField _lblMatOnline;

HorizontalFieldManager _fieldManagerOnline;

 

and adding them by:

 

_fieldManagerOnline.add(_lblMatOnline);

this.add(_fieldManagerOnline);

this.add(new SeparatorField());

 

 

 

btw.

Curve 8300 + JDE4.5(eclipse)

 

Thanks,

Dave

Developer
Posts: 984
Registered: ‎02-10-2009
My Device: Not Specified

Re: How to push second screen?

It seems your MatOnline Screen is ok.

 

Can you paste your both Screens that you are trying.

Then it would be easier to find out where the problem can be.

 

 

Regards

Bikas 

Developer
Posts: 25
Registered: ‎01-29-2009
My Device: Curve 8330

Re: How to push second screen?

[ Edited ]
The Welcome Screen is :

public class WelcomeScreen extends MainScreen{ LabelField _lblWelcome, _lblConn,_lblMatTitle; HorizontalFieldManager _fieldManagerTop, _fieldManagerBottom, _fieldManagerTitle, _fieldManagerMiddle2; VerticalFieldManager _fieldManagerMiddle; BitmapField _bitmap, _bitmapTitle; Bitmap _mat, _MatTitle; String _Welcome, _Scan; private BluetoothSerialPortInfo[] _portInfo; public WelcomeScreen() { _mat = Bitmap.getBitmapResource("Mat.png"); _MatTitle = Bitmap.getBitmapResource("MatAppFocus.png"); _fieldManagerTitle = new HorizontalFieldManager(); _bitmapTitle = new BitmapField(); _bitmapTitle.setBitmap(_MatTitle); _lblMatTitle = new LabelField("Mat", Field.FIELD_BOTTOM); _fieldManagerTitle.add(_bitmapTitle); _fieldManagerTitle.add(_lblMatTitle); setTitle(_fieldManagerTitle); _fieldManagerTop = new HorizontalFieldManager(); _fieldManagerMiddle = new VerticalFieldManager(NO_HORIZONTAL_SCROLL | NO_VERTICAL_SCROLL| VerticalFieldManager.FIELD_HCENTER); _fieldManagerMiddle2 = new HorizontalFieldManager(); _fieldManagerBottom = new HorizontalFieldManager(); this.add(_fieldManagerTop); this.add(new SeparatorField()); this.add(_fieldManagerMiddle); this.add(_fieldManagerMiddle2); this.add(new SeparatorField()); this.add(_fieldManagerBottom); //Top _lblWelcome = new LabelField("Welcome"); _fieldManagerTop.add(_lblWelcome); //Middle _bitmap = new BitmapField(); _bitmap.setBitmap(_mat); _lblConn = new LabelField("Connect"); _fieldManagerMiddle.add(_bitmap); _fieldManagerMiddle2.add(_lblConn); FieldChangeListener scanListener = new FieldChangeListener(){ public void fieldChanged(Field field, int context){ Invoke.invokeApplication(Invoke.APP_TYPE_BLUETOOTH_CONFIG, null ); } }; FieldChangeListener quitListener = new FieldChangeListener(){ public void fieldChanged(Field field, int context){ System.exit(0); } }; //////////////////////////////////////////////////////////// // Menu Item // //////////////////////////////////////////////////////////// ButtonField scanButton = new ButtonField(" New"); ButtonField quitButton = new ButtonField(" Quit "); scanButton.setChangeListener(scanListener); quitButton.setChangeListener(quitListener); _fieldManagerBottom.add(scanButton); _fieldManagerBottom.add(quitButton); } protected void makeMenu(Menu menu, int instance) { int numDevices = 0; if (BluetoothSerialPort.isSupported()) { _portInfo = BluetoothSerialPort.getSerialPortInfo(); numDevices = _portInfo.length; DeviceMenuItem deviceItems[] = new DeviceMenuItem[numDevices]; for (int count = numDevices - 1; count >= 0; --count) { deviceItems[count] = new DeviceMenuItem("Connect to: " + _portInfo[count].getDeviceName(), _portInfo[count]); menu.add(deviceItems[count]); } } else { Dialog.alert("No Bluetooth Devices Found. Perform Scan"); } } final class DeviceMenuItem extends MenuItem { private BluetoothSerialPortInfo _info; DeviceMenuItem(String text, BluetoothSerialPortInfo info) { super(text, 20, 20); _info = info; } public void run() { MatMain.getUiApplication().pushScreen(new MatOnline(_info)); } }; public boolean onSavePrompt() { return true; } }

 


-------------------------------------------------------------------------------------------------------------------------------------
-------------------------------- the MatOnline Class is----------------------------------------------------------------------


class MatOnline extends MainScreen implements BluetoothSerialPortListener{ private StringBuffer _data; private byte[] _receiveBuffer = new byte[1024]; private String _dataReceived; private BluetoothSerialPort _port; // Define private static boolean _dataSent = true; private String _deviceName; private DataBuffer _db; // Define Screen objects LabelField _lblMatOnline; HorizontalFieldManager _fieldManagerOnline; BitmapField _s1; public MatOnline(BluetoothSerialPortInfo info){ //Commands used for communication //String[] _startSetup = new String[256]; String _ok ="ACK"; String _error ="ERR"; String _init ="INIT"; // Initialize the buffers. _data = new StringBuffer(); _db = new DataBuffer(); //Connect try { // Connect to the selected device. _port = new BluetoothSerialPort(info, BluetoothSerialPort.BAUD_9600, BluetoothSerialPort.DATA_FORMAT_PARITY_NONE | BluetoothSerialPort.DATA_FORMAT_STOP_BITS_1 | BluetoothSerialPort.DATA_FORMAT_DATA_BITS_8, BluetoothSerialPort.FLOW_CONTROL_NONE, 1024, 1024, this); _deviceName = info.getDeviceName(); } catch(IOException ex) { Status.show("Error: " + ex.getMessage()); } _lblMatOnline = new LabelField("Connecting..."); // Add menu items to the screen. Dialog.alert("Add stuff"); _fieldManagerOnline.add(_lblMatOnline); this.add(_fieldManagerOnline); this.add(new SeparatorField()); addMenuItem(_closeSP); //Close Port addMenuItem(_setup); //Setup device. } private MenuItem _closeSP = new MenuItem("Close Connection", 20, 20) { public void run() { if (_port != null) { _port.close(); } close(); } }; private MenuItem _setup = new MenuItem("Setup", 80, 80) { public void run() { // writeData(_startSetup, 0, _startSetup.length); //test } }; }

 

Message Edited by dmravyan on 05-26-2009 03:27 PM
Developer
Posts: 984
Registered: ‎02-10-2009
My Device: Not Specified

Re: How to push second screen?

Hi,

 

I think you are trying in a very complex way.

 

Using your complex architecture and ignoring the bluetooth related codes, I can see the your second screen with a minor change.

 

  In your DeviceMenuItem class,

      replaced

                             MatMain.getUiApplication().pushScreen(new MatOnline(_info));

     with

                             UiApplication.getUiApplication().pushScreen(new MatOnline(_info));
 

And in your MatOnline class:

    replaced

                            HorizontalFieldManager _fieldManagerOnline;
    with

                            HorizontalFieldManager _fieldManagerOnline =  new HorizontalFieldManager();

 

 

 

Regards

Bikas
 

Developer
Posts: 25
Registered: ‎01-29-2009
My Device: Curve 8330

Re: How to push second screen?

Thanks for your help! those changes did the trick.

 

I am new to BB development so if you have any suggestions regarding the use of the bluetooth module I would be glad to hear them.

 

Thanks again!

Dave