简体   繁体   English

J2ME转换器程序不接受用户输入

[英]J2ME converter program not accepting user's input

I am having problems with my converter program not accepting the value the user inputs. 我的转换器程序无法接受用户输入的值时遇到问题。 Everything seems so right but the result is always as if the user entered nothing. 一切似乎都很好,但是结果总是好像用户什么都不输入。 If anyone can just point me in the right direction I would greatly appreciate it! 如果有人能指出正确的方向,我将不胜感激!

my project consists of 2 files (1 midlet and 1 class). 我的项目包含2个文件(1个Midlet和1个类)。

code was too long to post on this forum so I uploaded the zip. 代码太长,无法在此论坛上发布,因此我上传了zip。

Edit hosted here now should be cleaner: removed I can't really narrow it down to a small piece of code because it could be any number of things which I have already tried. 现在,这里托管的Edit应该更简洁:删除了,我无法真正将其缩小为一小段代码,因为它可能是我已经尝试过的许多东西。 I know its asking quite a bit but the code isn't insanely long. 我知道它的问题很多,但是代码并不疯狂。 I'd be extremely grateful if anyone could take a look. 如果有人可以看看,我将不胜感激。

edit 2: the file seems to be trying to download an image... here is the code in another forum i posted in but got no answers.: http://www.codingforums.com/showthread.php?p=1024059#post1024059 编辑2:该文件似乎正在尝试下载图片...这是我在另一个论坛中发布的代码,但没有答案。: http : //www.codingforums.com/showthread.php? p =1024059#第1024059章

edit 3: here is where I think the problem lies in the code:` public double customForm (String fName) { ui_form = new Form(fName); 编辑3:这是我认为问题出在代码中的地方:`public double customForm(String fName){ui_form = new Form(fName);

    ui_form.addCommand(Convert);
    ui_form.addCommand(Back);

    display.setCurrent(ui_form);

num = new TextField("Enter the number of units you would like to convert", "", MAX_CHARS, TextField.ANY);
    ui_form.append(num);
    ui_form.setCommandListener(this);

    /***********************/
    /* THIS IS NOT WORKING*/
   // str = num.getString();
    str = "The number is: " + num.getString();
    try
    {
    numUnits = Double.parseDouble(str);
    }
    catch (NumberFormatException nfe)
    {
    }
    finally
    {
        return numUnits;
    }

    //return str;
}

` `

but you will probably need to look at the rest of the code to see where the problem lies. 但是您可能需要查看其余的代码以查看问题所在。 this is just the root i think 这只是我认为的根源

You cannot read the text field right after it has been added to a form. 将文本字段添加到表单后,您将无法立即阅读。 It looks like you assumed the num.getString() method to block until there's user input - this is not the case. 似乎您假设num.getString()方法在用户输入之前会一直阻塞-事实并非如此。 User input is provided asynchronously , ie you should read the text field's content in response to a user command, somewhere in the commandAction() method (in your case when the Convert command has been issued by the user). 用户输入是异步提供的,即,您应该在commandAction()方法中的某个位置(对于您的情况是由用户发出了Convert命令commandAction() ,应根据用户命令读取文本字段的内容。

Handling user events asynchronously is a core pattern in GUI development. 异步处理用户事件是GUI开发中的核心模式。 In this regard I recommend to read some example code for command handling in JavaME, for instance this or this . 在这方面,我建议阅读一些JavaME中用于命令处理的示例代码,例如thisthis

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM