gpt4 book ai didi

java - 变量无法解析

转载 作者:行者123 更新时间:2023-11-29 03:51:58 25 4
gpt4 key购买 nike

我正在阅读 Beginning BlackBerry 7 Development 这本书,并真正将我很少或根本不存在的 oop/java 技能用于测试 - eclipse 从书中的代码中抛出一个错误,指出 loginhandler 尚未声明- 是的,它是正确的 - 它没有被宣布,但它也没有在书中被宣布。

已经完成的是一个名为 logincommandhandler 的内部类(在我的代码底部),可以在此处的摘录中看到 - http://my.safaribooksonline.com/book/-/9781430230151/chapter-4-user-interface-basics/67 - 这就是调用的意思(我假设)但是我有限的 oop 技能我不知道 loginHandler 应该是什么 - 它应该用类型定义在某个地方吗?

(本书无勘误)

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


}

和错误-

LoginHandler cannot be resolved to a variable   UiFunMainScreen.java    /UiFun/src/com/beginningblackberry/uifun    line 69 Java Problem

有没有 blackberry/java 高手指出我哪里出错了?

更新

没有人真正回答这个问题 - 调用新的内部类,我改为调用它

MenuItem loginMenu = new MenuItem(new StringProvider("Login"),20,10);
loginMenu.setCommand(new Command(new LoginCommandHandler()));
menu.add(loginMenu);

更新 2 秒答案

将 loginHandler 声明为类变量也有效 -

LoginCommandHandler loginHandler = new LoginCommandHandler();

最佳答案

我的猜测是它应该是 LoginCommandHandler 的实例(最后声明的类)。这是一个猜测,但是一个有根据的猜测:Command 的构造函数需要一个 CommandHandler 实例,并且 LoginCommandHandler 扩展 CommandHandler , 所以...

最简单的改变是改变所有看起来像这样的地方:

loginButton.setCommand(new Command(LoginHandler));

到:

loginButton.setCommand(new Command(new LoginCommandHandler()));

例如,我们正在调用 LoginCommandHandler 构造函数并将生成的对象传递给 new Command()

或者如果出于某种原因(我还没有真正阅读代码)您需要对处理程序的引用,声明并实例化它:

LoginCommandHandler loginHandler = new LoginCommandHandler();

...然后使用它

loginButton.setCommand(new Command(loginHandler));

(请注意,他们在引用的代码中有多个地方犯了这个错误。)

关于java - 变量无法解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8342344/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com