JVM exception in simple program - blackberry

I am writing a small demo program to display a message box in to run in blackberry. As soon as I click the OK button on the dialog, it throws me a JVM Error 104, uncaught:runtimeexception. Any help please? Here is the code :
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.HorizontalFieldManager;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.ui.container.VerticalFieldManager;
import net.rim.device.api.ui.*;
import net.rim.device.api.ui.container.*;
public class My_First_App extends UiApplication
{
public static void main(String[] args)
{
My_First_App theApp = new My_First_App();
theApp.enterEventDispatcher();
}
public My_First_App()
{
pushScreen(new My_First_AppScreen());
}
}
final class My_First_AppScreen extends MainScreen
{
public My_First_AppScreen()
{
super();
HorizontalFieldManager _fieldmanager;
_fieldmanager = new HorizontalFieldManager();
/* declare one label to how the application title */
LabelField applicationtitle = new LabelField("Demo",LabelField.ELLIPSIS | LabelField.USE_ALL_WIDTH);
ButtonField _pairMe = new ButtonField("PairMe");
FieldChangeListener listenerPairMe = new FieldChangeListener() {
public void fieldChanged(Field field, int context){
Dialog.alert("You clicked the button!");
}
};
/*set the title*/
add(_fieldmanager);
setTitle(applicationtitle);
_fieldmanager.add(_pairMe);
_pairMe.setChangeListener(listenerPairMe);
}
public boolean onClose()
{
Dialog.alert("Goodbye!");
System.exit(0);
return true;
}
}

create a new workspace. It's related to Eclipse, not to your code

Related

I need my Screen to wait for a thread to end before being pushed

I need my app to start a thread (after clicking on a button) that does something, get me some variables then push a screen that displays those variables. The problem is that I can't get my screen to wait for that thread. Every time I run the app, I have to refresh the new screen to see the variables values. How should I deal with that? I used the invokeLater() method but that doesn't seem to work!
Here is my first screen's code:
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.ButtonField;
public class MyScreen extends MainScreen implements FieldChangeListener
{
public MyScreen()
{
super();
setTitle("MyScreen!");
ButtonField bf = new ButtonField("Start thread", ButtonField.CONSUME_CLICK) ;
this.add(bf);
bf.setChangeListener(this);
}
public void fieldChanged(Field Field, int context) {
MyThreadClass myThread = new MyThreadClass();
myThread.start();
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
UiApplication.getUiApplication().pushScreen(new NewScreen());
}
});
}
}
Where did I go wrong?
try this -
MyThreadClass myThread = new MyThreadClass();
myThread.start();
and
class MyThreadClass implements Runnable {
public void run() {
// here - your code for getting variables.
// After getting your variable , push the screen.
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
UiApplication.getUiApplication().pushScreen(new NewScreen());
}
});
}
}

How to use Telugu font in blackberry application

As I'm novice in Blackberry/Java development, I don't know how to use "telugu" font in my Blackberry application. Please give an example.
see the difference between three simulators
9300-OS6 not displaying
9790-os7 displays only in this device .
9900-os7 even OS7 it is not displaying.
import net.rim.device.api.system.*;
import net.rim.device.api.ui.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.ui.component.*;
import java.util.*;
public class FontLoadingDemo extends UiApplication
{
public static void main(String[] args)
{
FontLoadingDemo app = new FontLoadingDemo();
app.enterEventDispatcher();
}
public FontLoadingDemo()
{
pushScreen(new FontLoadingDemoScreen());
}
}
class FontLoadingDemoScreen extends MainScreen
{
public FontLoadingDemoScreen()
{
setTitle("Font Loading Demo");
LabelField helloWorld = new LabelField("Hello World");
if (FontManager.getInstance().load("Myfont.ttf", "MyFont", FontManager.APPLICATION_FONT) == FontManager.SUCCESS)
{
try
{
FontFamily typeface = FontFamily.forName("MyFont");
Font myFont = typeface.getFont(Font.PLAIN, 50);
helloWorld.setFont(myFont);
}
catch (ClassNotFoundException e)
{
System.out.println(e.getMessage());
}
}
add(helloWorld);
}
}
Source: http://docs.blackberry.com/en/developers/deliverables/11958/Load_and_use_a_custom_font_899948_11.jsp

Why button on screen not effect when I use postGlobalScreen to push this screen?

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());
}
}
}

Hide EditField on Radiobutton click Blackberry

I have two Radiobutton and on one RadioButton click i want to hide editfield, so please can anyone help me.
Thanks, In advance.
Perhaps you could try using Managers to delete and add fields. Try:
package mypackage;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.component.BasicEditField;
import net.rim.device.api.ui.component.RadioButtonField;
import net.rim.device.api.ui.component.RadioButtonGroup;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.ui.container.VerticalFieldManager;
public final class MyScreen extends MainScreen {
private BasicEditField bef = new BasicEditField();
private VerticalFieldManager manager = new VerticalFieldManager();
private RadioButtonGroup rbg = new RadioButtonGroup();
public MyScreen() {
setTitle("Hide Control Demo");
manager.add(bef);
add(manager);
RadioButtonField rbf1 = new RadioButtonField("Option 1", rbg, true);
RadioButtonField rbf2 = new RadioButtonField("Option 2", rbg, false);
add(rbf1);
add(rbf2);
rbf2.setChangeListener(new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
if (bef.getManager() != null && rbg.getSelectedIndex() == 1) {
manager.delete(bef);
System.out.println("Option 2 Selected");
}
}
});
}
}
Try this it will work
package com.hb;
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.EditField;
import net.rim.device.api.ui.component.RadioButtonField;
import net.rim.device.api.ui.component.RadioButtonGroup;
import net.rim.device.api.ui.container.HorizontalFieldManager;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.ui.container.VerticalFieldManager;
public class Startup extends UiApplication{
public static void main(String[] args) {
Startup start=new Startup();
start.enterEventDispatcher();
}
public Startup() {
pushScreen(new screen());
}
}
class screen extends MainScreen implements FieldChangeListener
{
RadioButtonField field1,field2;
private EditField edit1;
private VerticalFieldManager edit_mgr=null;
public screen() {
HorizontalFieldManager hr=new HorizontalFieldManager();
RadioButtonGroup g=new RadioButtonGroup();
field1=new RadioButtonField("edit", g,true);
field1.setChangeListener(this);
hr.add(field1);
field2=new RadioButtonField("no edit", g,false);
field2.setChangeListener(this);
hr.add(field2);
add(hr);
edit_mgr=new VerticalFieldManager();
edit1=new EditField();
edit1.setEditable(true);
edit_mgr.add(edit1);
add(edit_mgr);
}
public void fieldChanged(Field field, int context) {
if(field==field1){
synchronized (UiApplication.getEventLock()) {
edit1.setEditable(true);
}
}else if(field==field2){
synchronized (UiApplication.getEventLock()) {
edit1.setEditable(false);
}
}
}
}

Blackberry: display Alert/Status/Dialog early and exit

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);
}
}
}

Resources