My application extends the ui.Manager class. Is it possible to enter the app without using the enterEventDispatcher. As it needs me to inherit the Application/UiApplication to do that.
Is multiple inheritance the solution?
Multiple inheritance is rather not possible in Java. So is it about using interfaces?
You should really inherit App from UiApplication (if it has some UI) or from Application (if it's background app, service).
If you have some Manager extension, place it into Screen extension.
Like this:
import net.rim.device.api.system.Display;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.container.HorizontalFieldManager;
import net.rim.device.api.ui.container.MainScreen;
public class CenterButtonPanelApp extends UiApplication {
public CenterButtonPanelApp() {
pushScreen(new Scr());
}
public static void main(String[] args) {
CenterButtonPanelApp app = new CenterButtonPanelApp();
app.enterEventDispatcher();
}
}
class Scr extends MainScreen {
public Scr() {
CenterButtonPanel centerPanel = new CenterButtonPanel();
add(centerPanel);
}
}
class CenterButtonPanel extends HorizontalFieldManager {
int mWidth = Display.getWidth();
public CenterButtonPanel() {
super(FIELD_HCENTER);
}
public int getPreferredWidth() {
return mWidth;
}
protected void sublayout(int maxWidth, int maxHeight) {
super.sublayout(mWidth, maxHeight);
setExtent(mWidth, maxHeight);
}
}
Related
Eclipse keeps telling me about invoke problems. This is the message i get as can be seen below. Please help me resolve this problem. What code should i put to get rid of the invoke problem. thanks.
Warning!: method 'parsepack.xmlparsing.navigationClick(int,int)' not invoked.
Warning!: method 'parsepack.xmlparsing.insert(String,int)' not invoked.
here is the method navigationClick()
protected boolean navigationClick(int status, int time)
{
try
{
//navigate here to another screen
ui.pushScreen(new ResultScreen());
}
catch(Exception e)
{
System.out.println("Exception:- : navigationClick() "+e.toString());
}
return true;
}
here is the method insert()
public void insert(String toInsert, int index)
{
listElements.addElement(toInsert);
}
here is the class xmlparsing.java
package parsepack;
import java.io.IOException;
import java.io.InputStream;
import java.util.Vector;
import javax.microedition.io.Connector;
import javax.microedition.io.StreamConnection;
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.system.Display;
import net.rim.device.api.ui.DrawStyle;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.Manager;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.ListField;
import net.rim.device.api.ui.component.ListFieldCallback;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.ui.container.VerticalFieldManager;
import net.rim.device.api.xml.parsers.DocumentBuilder;
import net.rim.device.api.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class xmlparsing extends UiApplication implements ListFieldCallback, FieldChangeListener
{
public static void main(String[] args)
{
xmlparsing app = new xmlparsing();
app.enterEventDispatcher();
}
public long mycolor ;
Connection _connectionthread;
private static ListField _list;
private static Vector listElements = new Vector();
public MainScreen screen = new MainScreen();
VerticalFieldManager mainManager;
VerticalFieldManager subManager;
UiApplication ui = UiApplication.getUiApplication();
public xmlparsing()
{
super();
pushScreen(screen);
final Bitmap backgroundBitmap = Bitmap.getBitmapResource("blackbackground.png");
mainManager = new VerticalFieldManager(Manager.NO_VERTICAL_SCROLL | Manager.NO_VERTICAL_SCROLLBAR )
{
public void paint(Graphics graphics)
{
graphics.drawBitmap(0, 0, Display.getWidth(),Display.getHeight(),backgroundBitmap, 0, 0);
super.paint(graphics);
}
};
subManager = new VerticalFieldManager(Manager.VERTICAL_SCROLL | Manager.VERTICAL_SCROLLBAR )
{
protected void sublayout( int maxWidth, int maxHeight )
{
int displayWidth = Display.getWidth();
int displayHeight = Display.getHeight();
super.sublayout( displayWidth, displayHeight);
setExtent( displayWidth, displayHeight);
}
};
screen.add(mainManager);
_list = new ListField()
{
public void paint(Graphics graphics)
{
graphics.setColor((int) mycolor);
super.paint(graphics);
}
};
mycolor = 0x00FFFFFF;
_list.invalidate();
_list.setEmptyString("* Feeds Not Available *", DrawStyle.HCENTER);
_list.setRowHeight(50);
_list.setCallback(this);
mainManager.add(subManager);
listElements.removeAllElements();
_connectionthread = new Connection();
_connectionthread.start();
}
protected boolean navigationClick(int status, int time)
{
try
{
//navigate here to another screen
ui.pushScreen(new ResultScreen());
}
catch(Exception e)
{
System.out.println("Exception:- : navigationClick() "+e.toString());
}
return true;
}
private class Connection extends Thread
{
public Connection()
{
super();
}
public void run() {
Document doc;
StreamConnection conn = null;
InputStream is = null;
try {
conn = (StreamConnection) Connector.open("http://imforchange.org/international-movement-for-change/testing/data.xml"+";deviceside=true");
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
docBuilderFactory.setIgnoringElementContentWhitespace(true);
docBuilderFactory.setCoalescing(true);
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
docBuilder.isValidating();
is = conn.openInputStream();
doc = docBuilder.parse(is);
doc.getDocumentElement().normalize();
NodeList list = doc.getElementsByTagName("eventName");
for (int i = 0; i < list.getLength(); i++)
{
Node textNode = list.item(i).getFirstChild();
listElements.addElement(textNode.getNodeValue());
}
} catch (Exception e) {
System.out.println(e.toString());
} finally {
if (is != null) {
try {
is.close();
} catch (IOException ignored) {
}
}
if (conn != null) {
try {
conn.close();
}
catch (IOException ignored) {
}
}
}
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
_list.setSize(listElements.size());
subManager.add(_list);
screen.invalidate();
}
});
}
}
public void drawListRow(ListField list, Graphics g, int index, int y, int w)
{
String tes = (String)listElements.elementAt(index);
int yPos = 0+y;
g.drawLine(0, yPos, w, yPos);
g.drawText(tes, 5, 15+y, 0, w);
}
public Object get(ListField list, int index)
{
return listElements.elementAt(index);
}
public int indexOfList(ListField list, String prefix, int string)
{
return listElements.indexOf(prefix, string);
}
public int getPreferredWidth(ListField list)
{
return Display.getWidth();
}
public void insert(String toInsert, int index) {
listElements.addElement(toInsert);
}
public void fieldChanged(Field field, int context) {
}
}
ResultScreen.java
package parsepack;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.container.MainScreen;
public class ResultScreen extends MainScreen {
public ResultScreen() {
LabelField resultLabel = new LabelField("Result: ");
add(resultLabel);
}
}
First of all, those messages you're seeing are warnings, not errors. Warnings are not always a problem, but sometimes they are. In this case, I think that at least the first message is a problem, so you'll want to fix it.
1) First, the navigationClick() method. The compiler is telling you that you have an implementation for the method navigationClick() that is never called from any of your code, or by the BlackBerry OS infrastructure. That's probably not what you want. Normally, navigationClick() is a method that you override in a class you write that extends one of the BlackBerry Field classes. For example, a ButtonField subclass. navigationClick() will be called, in that case, when the button is clicked.
But, you placed your navigationClick() method in the main UIApplication subclass. That's not what you want. You need that method to override the navigationClick() method in a field class. I'm not sure which field you want to have call this method when the user clicks. But, for example, you might do something like this:
_list = new ListField()
{
public void paint(Graphics graphics)
{
graphics.setColor((int) mycolor);
super.paint(graphics);
}
protected boolean navigationClick(int status, int time)
{
try
{
//navigate here to another screen
ui.pushScreen(new ResultScreen());
}
catch(Exception e)
{
System.out.println("Exception:- : navigationClick() "+e.toString());
}
return true;
}
};
That would make navigationClick() get called when your list is clicked, and it will get rid of the warning.
2) Regarding the warning about insert(), that's just because you're not using it anywhere. It look like you've added that method to allow code outside the xmlparsing class to be able to insert items into the list. Maybe that's what you want, but you just haven't yet written the other code to use that method. I see you having at least a few choices:
remove or comment out the insert() method until you need it. this will get rid of the warning.
add some more code that does use this method.
ignore the warning, just making sure you understand that the warning is telling you that you have some code that is not (yet) necessary, since it's unused
you can suppress warnings in Java programs, by doing this, or this, in the Eclipse preferences, if you know the warning is not a problem, and you don't want excessive warnings to hide real problems. I normally don't recommend this. Usually, it's better to fix the warning, than to hide it.
Hi i have typed a code but it doesn't seem to work will you let me know what's the error?
import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FocusChangeListener;
import net.rim.device.api.ui.Font;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.Manager;
import net.rim.device.api.ui.component.BasicEditField;
import net.rim.device.api.ui.component.EditField;
import net.rim.device.api.ui.container.VerticalFieldManager;
public class CustomTextField extends VerticalFieldManager implements FocusChangeListener{
private int textWidth=0;
private Font font=Font.getDefault();
private int textHeight=0;
public EditField basicEditField;
public CustomTextField(int width,int height) {
textWidth=width;
textHeight=height;
VerticalFieldManager vfm=new VerticalFieldManager(Manager.FOCUSABLE);
basicEditField=new EditField(null,null,200, Field.EDITABLE|Field.FOCUSABLE|BasicEditField.NO_NEWLINE);
basicEditField.setFocusListener(this);
vfm.add(basicEditField);
add(vfm);
}
protected void sublayout(int maxWidth, int maxHeight) {
if(textWidth==0)
{
textWidth=maxWidth;
}
if(textHeight==0)
{
textHeight=maxHeight;
}
super.layout(textWidth, textHeight);
setExtent(textWidth, textHeight);
}
protected void paint(Graphics graphics) {
graphics.setColor(Color.BLACK);
graphics.drawRect(basicEditField.getLeft(),basicEditField.getTop(), textWidth, textHeight);
}
public void setHeight(int height) {
textHeight= height;
}
public void setWidth(int width) {
textWidth= width;
}
public Font getFont() {
return font;
}
public void setFont(Font font) {
this.font = font;
}
public void focusChanged(Field field, int eventType) {
if(eventType==FOCUS_GAINED)
{
if(field==basicEditField)
{
basicEditField.setCursorPosition(basicEditField.getText().length());
}
}
}
}
The typed thing doesn't get visible on the Screen
I have just created a CustomTextField which extends VerticalFieldManager
inside it
I have added an EditField which gets added in VerticalFieldManager
But it does'nt work
I mean it doesn't show what i type on the screen
What's the error can you check it out
I do invoke the CustomTextField using
CustomTextField ctf=new CustomTextField(100,200):
check
protected void paint(Graphics graphics)
add
super.paint(g);
at the end in it.
I edit Blackberry_App_Descriptor.xml to make my app is "Auto run on start up". My app have 3 classes, myApp, screen1 and screen2. MyApp with main method to push screen1. in screen1 have a button to push screen 2. It run OK when I launch appplication manualy. (click icon of App )
Problem is:
I use RealTimeListener to always check time each minutes, if it is 1h30 it will push screen1, ( I used method postGlobalScreen to push Screen1 ). And it pushed success. But i can use the button on this screen1, I clicked it and it not push to screen2.
I try to use Alternate Entry point to check time and push screen1 but it have the same result.
Can anybody help me solve and explain clearly about this problem ?
// MyApp.java
public class MyApp extends UiApplication implements RealtimeClockListener
{
/**
* Entry point for application
* #param args Command line arguments (not used)
*/
public static void main(String[] args)
{
// Create a new instance of the application and make the currently
// running thread the application's event dispatch thread.
MyApp theApp = new MyApp();
theApp.enterEventDispatcher();
}
/**
* Creates a new MyApp object
*/
public MyApp()
{
// Push a screen onto the UI stack for rendering.
pushScreen(new Screen1());
addRealtimeClockListener(this);
}
public void clockUpdated() {
int hour = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
int minute = Calendar.getInstance().get(Calendar.MINUTE);
if(hour==1 && minute == 30){
UiApplication.getUiApplication().pushGlobalScreen(new Screen1(),1,UiEngine.GLOBAL_MODAL);
}
}
}
//Screen1.java
public final class Screen1 extends MainScreen implements FieldChangeListener
{
/**
* Creates a new MyScreen object
*/
ButtonField button;
public Screen1()
{
button = new ButtonField("Screen 1 ");
button.setChangeListener(this);
add(button);
}
public void fieldChanged(Field field, int context) {
if(field==button){
UiApplication.getUiApplication().pushScreen(new Screen2());
}
}
}
//Screen2.java
public final class Screen2 extends MainScreen implements FieldChangeListener
{
/**
* Creates a new MyScreen object
*/
ButtonField button;
public Screen2()
{
button = new ButtonField("Screen2");
button.setChangeListener(this);
add(button);
}
public void fieldChanged(Field field, int context) {
if(field==button){
UiApplication.getUiApplication().pushScreen(new Screen1());
}
}
}
Please try following. here concentrate on two points
1)Application run in background background
2)send background application into foreground
package mypackage;
import java.util.Calendar;
import net.rim.device.api.system.RealtimeClockListener;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.UiEngine;
public class MyApp extends UiApplication implements RealtimeClockListener
{
/**
* Entry point for application
* #param args Command line arguments (not used)
*/
public static MyApp theApp=null;
public static void main(String[] args)
{
// Create a new instance of the application and make the currently
// running thread the application's event dispatch thread.
theApp = new MyApp();
theApp.enterEventDispatcher();
}
/**
* Creates a new MyApp object
*/
public MyApp()
{
// Push a screen onto the UI stack for rendering.
pushScreen(new Screen1());
addRealtimeClockListener(this);
}
public void clockUpdated() {
int hour = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
int minute = Calendar.getInstance().get(Calendar.MINUTE);
// if(hour==1 && minute == 30){
if(!theApp.isForeground())
{
UiApplication.getUiApplication().pushGlobalScreen(new Screen1(),1,UiEngine.GLOBAL_MODAL);
}
}
}
screen1.java
package mypackage;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.container.MainScreen;
public final class Screen1 extends MainScreen implements FieldChangeListener
{
/**
* Creates a new MyScreen object
*/
ButtonField button;
public Screen1()
{
button = new ButtonField("Screen 1 ");
button.setChangeListener(this);
add(button);
}
public void fieldChanged(Field field, int context) {
if(field==button){
close();
MyApp.theApp.requestForeground();
UiApplication.getUiApplication().pushScreen(new Screen2());
}
}
public boolean onClose() {
MyApp.theApp.requestBackground();
return true;
}
}
screen2.java
package mypackage;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.container.MainScreen;
public final class Screen2 extends MainScreen implements FieldChangeListener
{
/**
* Creates a new MyScreen object
*/
ButtonField button;
public Screen2()
{
button = new ButtonField("Screen2");
button.setChangeListener(this);
add(button);
}
public void fieldChanged(Field field, int context) {
if(field==button){
UiApplication.getUiApplication().pushScreen(new Screen1());
}
}
}
Let's say I have a typical Blackberry app:
public class MyApp extends UiApplication {
public static void main(String[] args) {
MyApp app = new MyApp();
app.enterEventDispatcher();
}
public MyApp() {
pushScreen(new MyScreen());
}
}
and already at the beginning I notice, that a mandatory condition is missing (wrong Display dimensions; missing SD card; some IT policy; etc.)
Is there a way to display a short and quick message to the user (in the form of Alert/Status/Dialog/whatever) and exit straight away - without/before instantiating a complex Screen/registering Acceleration listeners/installing complex CleanupRunnable?
I've tried Status.show(), Dialog.alert() - they do not work (RuntimeException "pushModalScreen called by a non-event thread"):
public class MyScreen extends MainScreen {
public MyScreen() {
if (Display.getWidth() < 400) {
Status.show("Goodbye");
return;
}
}
}
Instead of direct invocation use invokeLater. Sample is below:
Application.getApplication().invokeLater(new Runnable() {
public void run() {
Dialog.inform("Your message here...");
}
});
Instead of Dialog.inform you may use Status.show()
Actually the following is better, than what's suggested by Rafael - because it doesn't have the ugly white screen underneath. Here is my complete example MyApp.java:
import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.ui.decor.*;
import net.rim.device.api.system.*;
import net.rim.device.api.ui.image.*;
public class MyApp extends UiApplication {
public static void main(String[] args) {
MyApp app = new MyApp();
app.enterEventDispatcher();
}
public MyApp() {
pushScreen(new MyScreen());
}
}
class MyScreen extends MainScreen implements DialogClosedListener {
Dialog myDialog = new Dialog(Dialog.OK, "Goodbye!", 0, Bitmap.getPredefinedBitmap(Bitmap.EXCLAMATION), Dialog.GLOBAL_STATUS);
public MyScreen() {
// XXX just some condition, like wrong dimensions or IT policy
if (Display.getWidth() > 40) {
myDialog.setDialogClosedListener(this);
Application.getApplication().requestBackground();
Application.getApplication().invokeLater(new Runnable() {
public void run() {
myDialog.show();
}
});
return;
}
// XXX heavy stuff to be skipped
}
public void dialogClosed(Dialog dialog, int choice) {
if (dialog == myDialog) {
System.out.println("XXX exiting XXX");
System.exit(1);
}
}
}
(public class GPSDemo extends UiApplication) its a class name
i want to use this class in this code. i am make a object of this class but throw the exception
here is the place where we use it
popRunnable = new Runnable()
{
public void run()
{
/*StartUpScreen mainScreen = new StartUpScreen();
pushScreen(mainScreen); */
GPSDemo mainScreen =new GPSDemo();
// mainScreen.enterEventDispatcher();
//pushScreen(mainScreen);
}
};
who we use it
UiApplication and MainScreen are different things
public class Main extends UiApplication{
Main() {
pushScreen(new GPSDemo());
}
public static void main(String args[]) {
Main app = new Main();
app.enterEventDispatcher();
}
class GPSDemo extends MainScreen {
GPSDemo() {
this.setTitle("GPSDemo");
}
}