Trying to write a hello world CLDC blackberry program - blackberry

I got the book Advance Black Berry 6 Development.
I was able to get the midlet example to work, but not the first example for a CLDC program. It seems like it never gets to the code and when I run the app I get a blank white screen.
I tried to put a break point but it never went off.
Here is the code
package test.org;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;
public class cHelloUniverses extends UiApplication{
public static void main(String[] args)
{
(new cHelloUniverses()).start();
}
public void start()
{
MainScreen main = new MainScreen();
LabelField label= new LabelField("Hello Ted");
main.add(label);
UiApplication app = UiApplication.getUiApplication();
app.pushScreen(main);
app.enterEventDispatcher();
}
}

Replace your start() method with this:
public void start()
{
MainScreen main = new MainScreen();
LabelField label= new LabelField("Hello Ted");
main.add(label);
this.pushScreen(main);
this.enterEventDispatcher();
}

Related

How to make a button to do something in Blackberry

I am trying to make a button that shows a message when it is pressed, but cannot get it to work.
Here is my button:
HorizontalFieldManager buttonManager =
new HorizontalFieldManager(ButtonField.FIELD_HCENTER);
messageButton = new ButtonField("Press me", ButtonField.CONSUME_CLICK);
messageButton.setChangeListener(this);
buttonManager.add(messageButton);
add(buttonManager);
And here is the method that prints the message:
public void fieldChanged(Field field, int context) {
if (field == messageButton) {
showMessage();
}
}
private void showMessage() {
Dialog.inform("The button was pressed");
}
Am I doing something wrong in the showMessage() method, or id the error elsewhere?
// just paste this class and run
package mypackage;
import java.io.IOException;
import javax.microedition.media.MediaException;
import javax.microedition.media.Player;
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.component.Dialog;
import net.rim.device.api.ui.container.HorizontalFieldManager;
import net.rim.device.api.ui.container.MainScreen;
public final class MyScreen extends MainScreen
{
public MyScreen()
{
// Set the displayed title of the screen
HorizontalFieldManager horizontalFieldManager= new HorizontalFieldManager();
ButtonField buttonField= new ButtonField("click to show dialog",Field.FIELD_VCENTER);
horizontalFieldManager.add(buttonField);
buttonField.setChangeListener(buttonchangelisners);
add(horizontalFieldManager);
}
private FieldChangeListener buttonchangelisners= new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
// TODO Auto-generated method stub
showDialog("show your message");
}
};
public void showDialog(final String message) {
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
Dialog.alert(message);
}
});
}
}
Check the event log (Alt + LGLG), I suspect you'll see an error about pushing a modal screen on a non-event thread. If that is the case just change showMessage to
private void showMessage()
{
UiApplication.getUiApplication.invokeLater(new Runnable()
{
public void run()
{
Dialog.inform("The button was pressed");
}
});
}
I think the problem might be the flag CONSUME_CLICK you are passing to the constructor. If you want to do something with the button this is certainly not a flag you'd want to use.
Other than this, I'd advise against using FieldChangeListener for fields. Maybe for buttons is ok, but for other components the fieldChanged method might be called without user interaction during layout. This is why you almost always want to filter out those calls whose second parameter (context in your code) is equals to FieldChangeListener.PROGRAMMATIC.
A better alternative is to override navigationClick, which also works both for tactile and regular screens.
invokeLater is nevertheless useful, because fieldChanged is invoked at some point within a chain of UI Engine actions, and you'd better let it finish the job before displaying the Dialog. I suspect not using invokeLater is the main reason for the problem.
CONSUME_CLICK is in fact necessary when using fieldChangeListener, otherwise context menu will be invoked. If you switch to using navigationClick within your button field, you can return true to achieve the same behavior (though I prefer using navigationUnclick).

Creating a blackberry app that displays ads

I've created a simple application that displays an advertisement. I used this article. I followed all steps but I get NoClassDefFound error on the BlackBerry 9900 simulator.
Update:
I have used preverify.exe to checks jar file to be compatible with net_rim_api.jar:
preverify -classpath "D:\Eclipse\plugins\net.rim.ejde.componentpack7.1.0_7.1.0.10\com
ponents\lib\net_rim_api" "net_rim_bbapi_adv_app.jar" "D:\Eclipse\plugins\net.rim.ejde.componentpack7.1.0_7.1.0.10\components\bin\output"
i am getting like this,what should i do?
Error preverifying class net.rimlib.blackberry.api.advertising.app.a
java/lang/NoClassDefFoundError: java/lang/Thread
Below, there's my code:
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.container.MainScreen;
import net.rimlib.blackberry.api.advertising.app.Banner;
public class AdDemo extends UiApplication{
public static void main(String[] args)
{
AdDemo theApp = new AdDemo();
theApp.enterEventDispatcher();
}
public AdDemo()
{
pushScreen(new AdDemoScreen());
}
}
class AdDemoScreen extends MainScreen{
public AdDemoScreen()
{
Banner bannerAd = new Banner(16741, null);
bannerAd.setMMASize(Banner.MMA_SIZE_EXTRA_LARGE);
add(bannerAd);
}
}
How can I overcome this issue?
Thank you.

Java Can't Find Symbol Errors

I can't figure out why I keep getting the following errors with the following code:
HelloWorldApp.java:9: pushScreen(net.rim.device.api.ui.Screen) in net.rim.device.api.ui.UiApplication cannot be applied to (com.beginningblackberry.helloworld.HelloWorldMainScreen)
pushScreen(mainScreen);
HelloWorldMainScreen.java:10: cannot find symbol
symbol : method add(net.rim.device.api.ui.component.LabelField)
location: class com.beginningblackberry.helloworld.HelloWorldMainScreen
add(labelField);
\\HelloWorldApp.java
package com.beginningblackberry.helloworld;
import net.rim.device.api.ui.UiApplication;
class HelloWorldApp extends UiApplication {
HelloWorldApp() {
HelloWorldMainScreen mainScreen = new HelloWorldMainScreen();
pushScreen(mainScreen);
}
public static void main(String[] args){
HelloWorldApp app = new HelloWorldApp();
app.enterEventDispatcher();
}
}
\\ HelloWorldMainScreen.java
package com.beginningblackberry.helloworld;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.ui.component.LabelField;
class HelloWorldMainScreen {
HelloWorldMainScreen() {
LabelField labelField = new LabelField("Hello World");
add(labelField);
}
}
in HelloWorldMainScreen you probably need to extend some other class or need to provide the implementation of the add()

BlackBerry app "no Application instance" error

I want this non-ui app to open from an icon press and then invoke the memopad to make a new memo.
But when I run it from the icon click I get,
""Uncaught exception: no Application instance""
What am I doing wrong? I extended the Application to say it is non-ui. The Invoke.invoke ... code is correct I know. It has something to do with the struct and instance of the app. But I'm stumped.
package mprn;
import net.rim.blackberry.api.invoke.*;
import net.rim.device.api.system.Application;
public class memopadrn extends Application
{
public static void main(String[] args)
{
Invoke.invokeApplication(Invoke.APP_TYPE_MEMOPAD, new MemoArguments(MemoArguments.ARG_NEW));
}
}
Your application never enters into the Event Dispatcher, try this (untested):
import net.rim.blackberry.api.invoke.Invoke;
import net.rim.blackberry.api.invoke.MemoArguments;
import net.rim.device.api.ui.UiApplication;
public class Memopadrn extends UiApplication {
public static void main(String[] args) {
new Memopadrn().enterEventDispatcher();
}
public Memopadrn() {
Invoke.invokeApplication(Invoke.APP_TYPE_MEMOPAD, new MemoArguments(MemoArguments.ARG_NEW));
System.exit(0);
}
}

How can I lock the keypad in Blackberry application using "lockSystem" method in J2ME?

How can I lock the keypad in Blackberry application using "lockSystem" method in J2ME ??
And also the brightness of the blackberry should reduce to Zero ??
its really easy. I know the answer. We can just use the method "lockSystem". I have coded as following to lock the keypad. It takes long time for me to find it, but u got this.
package net.asem;
import net.rim.device.api.system.ApplicationManager;
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.component.LabelField;
import net.rim.device.api.ui.container.MainScreen;
public class LockBlackberry extends UiApplication
{
public static void main(String[] args)
{
LockBlackberry lockB = new LockBlackberry();
lockB.enterEventDispatcher();
}
LockBlackberry()
{
pushScreen(new myBlackBerryClass());
}
}
final class myBlackBerryClass extends MainScreen implements FieldChangeListener<br>
{
LabelField title;
ButtonField btn1;
myBlackBerryClass()
{
LabelField title = new LabelField("Title : Locking the Device.",LabelField.USE_ALL_WIDTH | LabelField.USE_ALL_WIDTH);
setTitle(title);
btn1 = new ButtonField("KeyPad Loack ?",ButtonField.CONSUME_CLICK);
btn1.setChangeListener(this);
add(btn1);
}
public void fieldChanged(Field field, int context)
{
if(field == btn1)
{
Click();
}
}
private void Click()
{
ApplicationManager manager = ApplicationManager.getApplicationManager();
manager.lockSystem(true);
}
}

Resources