Question

I'm working in java me, I'm trying to switch between visual designs using ok Commands and back Commands. I have a form displayable which I named formA in my main class A.java and a formB in another class B.java. I used an ok Command in formA which on selection, is supposed to take the user to formB. At first when I tried to call the getFormB method from my main class, it didn't work(non-static method cannot be referenced from a static context).

I was adviced to create a reference to B.java so I added this line in my main class A.java

B b = new B(this);

now I could call the getFormB method from my commandAction in formA. At this point, everything worked well but when I tried to add a backCommand which is supposed to take me back to formA in A.java I get ds error again 'non-static method getFormA() cannot be referenced from a static context', so I tried creating a reference in B.java same way I did in A.java, here's the code:

A a = new A(); 

then in command action I did ds on the backCommand:

switchDisplayable(null, a.getFormA()); 

This time, it compiled without errors. But at runtime I get a SecurityException MIDlet Manager ERROR:

Illegal attempt to construct hello.A@e938beb1

'hello' is the package containing both java files.

Can anyone help me out?

Was it helpful?

Solution

It seems that your A class extends MIDlet. If this is the case you should not try to make a new instance of it.
You should add an A attribute to your B class and receive the instance as a constructor parameter or have a setter method.
With this you can call the getFormA() method from the attribute.
Update

public class A extends MIDlet {
  B b;

  public A() {
    b = new B(this);
  }
}

class B {
  A a;

  B(A a) {
    this.a = a;
  }

  public void commandAction(Command c, Displayable d) {
    switchDisplayable(null, a.getFormA());
  }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top