I’m working through the book Beginning BlackBerry 7 Development and really putting my little or non existent oop/java skills to the test- eclipse is throwing up an error from the code in the book stating that loginhandler hasn’t been declared- and yes its correct- it hasn’t been declared but it also hasn’t been declared in the book.
What has been done is an inner class called logincommandhandler (at the bottom of my code) which can be seen in an excerpt here- http://my.safaribooksonline.com/book/-/9781430230151/chapter-4-user-interface-basics/67 – this is what its meant to be calling (i assume) but my limited oop skills i do not know what loginHandler should be- should it be defined somewhere with a type?
(there is no erata for the book)
package com.beginningblackberry.uifun;
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.BitmapField;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.ui.container.HorizontalFieldManager;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.component.EditField;
import net.rim.device.api.ui.component.PasswordEditField;
import net.rim.device.api.ui.component.SeparatorField;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.CheckboxField;
import net.rim.device.api.ui.component.ObjectChoiceField;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.component.Menu;
import net.rim.device.api.ui.MenuItem;
import net.rim.device.api.util.StringProvider;
import net.rim.device.api.command.Command;
import net.rim.device.api.command.CommandHandler;
import net.rim.device.api.command.ReadOnlyCommandMetadata;
public class UiFunMainScreen extends MainScreen implements FieldChangeListener {
BitmapField img;
EditField usernameField;
PasswordEditField passwordField;
ObjectChoiceField domainField;
CheckboxField rememberCheckBox;
ButtonField clearButton, loginButton;
public UiFunMainScreen(){
Bitmap logoBitmap = Bitmap.getBitmapResource("img/upd8rLOGO.png");
img = new BitmapField(logoBitmap, Field.FIELD_HCENTER);
add(img);
add(new SeparatorField());
add(new LabelField("Please Enter Your Credentials:"));
usernameField = new EditField("Username:","");
passwordField = new PasswordEditField("Password:","");
domainField = new ObjectChoiceField("Domain",new String[] {"Home","Work"});
rememberCheckBox = new CheckboxField("Remember password",false);
add(usernameField);add(passwordField);
add(domainField);
add(rememberCheckBox);
add(new SeparatorField());
clearButton = new ButtonField("Clear",ButtonField.CONSUME_CLICK);
loginButton = new ButtonField("Login",ButtonField.CONSUME_CLICK);
HorizontalFieldManager buttonManager = new HorizontalFieldManager(Field.FIELD_RIGHT);
buttonManager.add(clearButton);
buttonManager.add(loginButton);
add(buttonManager);
clearButton.setChangeListener(this);
//loginButton.setChangeListener(this);
loginButton.setCommand(new Command(LoginHandler));
}
//routing
public void fieldChanged(Field field, int context){
if(field == clearButton){
clearTextFields();
}else if(field == loginButton){
login();
}
}
private void login(){
if(usernameField.getTextLength()== 0 || passwordField.getTextLength() == 0){
Dialog.alert("You must enter a username and password");
}
else
{
String username = usernameField.getText();
String selectedDomain = (String) domainField.getChoice(domainField.getSelectedIndex());
LoginSuccessScreen loginSuccessScreen = new LoginSuccessScreen(username, selectedDomain);
UiApplication.getUiApplication().pushScreen(loginSuccessScreen);
}
}
public void clearTextFields ()
{
usernameField.setText("");
passwordField.setText("");
}
protected void makeMenu(Menu menu, int instance){
super.makeMenu(menu, instance);
/*
menu.add(new MenuItem(new StringProvider("Login"),20,10){
public void run(){
login();
}
});
*/
//login menu item
MenuItem loginMenu = new MenuItem(new StringProvider("Login"),20,10);
loginMenu.setCommand(new Command(LoginHandler));
menu.add(loginMenu);
//clear text menu item
menu.add(new MenuItem(new StringProvider("Clear"),20,10){
public void run(){
clearTextFields();
}
});
}
class LoginCommandHandler extends CommandHandler
{
public void execute(ReadOnlyCommandMetadata metadata, Object context){
login();
}
}
}
and the error-
LoginHandler cannot be resolved to a variable UiFunMainScreen.java /UiFun/src/com/beginningblackberry/uifun line 69 Java Problem
any blackberry/java wizards shed some light on where i am going wrong?
Update
No one really answered the question bang on- to call the new inner class i called this instead
MenuItem loginMenu = new MenuItem(new StringProvider("Login"),20,10);
loginMenu.setCommand(new Command(new LoginCommandHandler()));
menu.add(loginMenu);
update 2 second answer
Declaring loginHandler as a class variable also works –
LoginCommandHandler loginHandler = new LoginCommandHandler();