编程语言
首页 > 编程语言> > java-无法在J2ME中打开网站链接

java-无法在J2ME中打开网站链接

作者:互联网

我正在开发一个简单的J2ME应用程序,并且我有一个StringItem在线链接到条款和条件页面.

我有StringItem设置,它带有下划线(给人以链接的感觉);但是当我单击它时,它不执行任何操作.

在我的代码下面找到:

public class mobiMidlet extends MIDlet implements CommandListener {

    private Display display;
    private TextField userName,password;
    public Form form;
    private Command login, register, forgot, terms, cancel;
    private Image img_error, img_login, img_register, img_forgot, img_terms;
    private String termsurl = "http://example.com/terms.php";
    private StringItem termsItem;

    public mobiMidlet() {
          form = new Form("Welcome to My App");

          termsItem = new StringItem("", "Terms and Conditions", Item.HYPERLINK);
          termsItem.setDefaultCommand(new Command("terms", Command.ITEM, 1));    

            ItemCommandListener listener = new ItemCommandListener() {
                    public void commandAction(Command cmd, Item item) {
                        if(cmd==terms)
                        {
                            try {
                                platformRequest(termsurl);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    }
                };

            termsItem.setItemCommandListener(listener);        

          userName = new TextField("LoginID:", "", 30, TextField.ANY);
          password = new TextField("Password:", "", 30, TextField.PASSWORD);
          cancel = new Command("Cancel", Command.CANCEL, 2);
          login = new Command("Login", Command.OK, 2);
          try{
            img_login = Image.createImage("/logo.jpg");
            img_register = Image.createImage("/error2.png");
            img_forgot = Image.createImage("/logo.jpg");
            img_register = Image.createImage("/error2.png");
          }catch(Exception e){
            System.out.println(e.getMessage());
          }  
    }


    public void startApp() {
          display = Display.getDisplay(this);
          form.append(termsItem);
          form.append(userName);
          form.append(password);
          form.addCommand(cancel);
          form.addCommand(login);
          form.setCommandListener(this);
          display.setCurrent(form);
    }

    public void commandAction(Command c, Displayable d) {
          String label = c.getLabel();
          if(label.equals("Cancel")) {
              destroyApp(true);
          } else if(label.equals("Login")) {
            validateUser(userName.getString(), password.getString());
          } 
    }
}

如何解决此问题,以便在单击条款和条件链接时可以在浏览器中打开页面?

解决方法:

您尚未初始化变量项,因此它保持为空.因此,条件cmd == terms始终为false,并且您永远不会输入if语句.

单独的行termItem.setDefaultCommand(new Command(“ terms”,Command.ITEM,1));到两个:

terms = new Command("terms", Command.ITEM, 1);
termsItem.setDefaultCommand(terms);

现在,您将有机会.
顺便说一句,为什么不调试您的程序?在模拟器中运行它,将断点放入commandAction中,看看会发生什么.

标签:http,java-me,midp,lcdui,java
来源: https://codeday.me/bug/20191201/2080202.html