问题
I'm working in java me. I created two visual designs, First.java and Second.java both of which contains form displayables.
I added an Ok command to a form in First.Java and the user is expected to switch to SecondForm in Second.java but I get this error non-static method getSecondForm() cannot be referenced from a static context
.
How can I fix this?
Both files are in the same package. Here's the source code
public void commandAction (Command command, Displayable displayable) {
if (displayable == firstForm) {
if (command == exitCommand) {
exitMIDlet();
} else if (command == okCommand) {
switchDisplayable(null, Second.getSecondForm());
}
}
}
回答1:
You need to change declaration of the method getSecondForm
in Second.java to make it static.
If it currently looks like
Displayable getSecondForm() //... whatever code is there
then after change it should look like
static Displayable getSecondForm() //... whatever code is there
Note above change will fix the non-static method getSecondForm...
error you mentioned but there is a chance that it will cause new compilation errors in file Second.java
. You will need to fix these new errors, most likely by adding couple static
keywords here and there, similar to how it is shown above.
Note this is java language basic stuff; if you get stuck fixing this, consider taking a break to study some tutorial for beginners.
来源:https://stackoverflow.com/questions/14068709/switch-to-a-visual-design-from-another-visual-design