(J2ME) get MIDlet reference from another class?

I am trying to create a class that can set what is on the screen (for example, set a form to display what it has ever been) outside the midlet class ( Main)
So I thought that I needed to enter and change a variable Main display, but I came up with an error.

Here is the whole program:

//Main.java
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class Main extends MIDlet {

    public Other othr = new Other(this);
    public Display display = Display.getDisplay(this);
    public void startApp() {
        display.setCurrent(othr);
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
    }
}


//Other.java
import javax.microedition.lcdui.*;
public class Other extends Canvas{

    Form a = new Form("a");
    public TextEdit(Main mc){
        //HERE IT IS!
        mc.display.getDisplay(mc).setCurrent(a);
        //If I comment out the above, I get no error.

    }
    protected void paint(Graphics g) {
         //Nothing yet
    }

}

And I always get the error message "Application unexpectedly left."

I also tried replacing mc.display.getDisplay(mc).setCurrent(a);with Display.getDisplay(mc).setCurrent(a);, then the error is not displayed, but form a is not displayed at all.

This is probably a stupid mistake, but I lost

What can I do?

+3
source share
2 answers

This is a small error in the code. Make changes to your code as shown below.

//Main.java
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class Main extends MIDlet {

    public Other othr ;
    public Display display ;
    public void startApp() {
         display= Display.getDisplay(this);
        othr=new Other(this);
        display.setCurrent(othr);
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
    }
}

, , , , .

,

//Other.java
import javax.microedition.lcdui.*;
public class Other {

    Form a ;
    public Other(Main mc){
        //HERE IT IS!
       a=new Form("a");
        Display.getDisplay(mc).setCurrent(a);
        //If I comment out the above, I get no error.

    }

}

/Other.java
import javax.microedition.lcdui.*;
public class Other extends Canvas{

     public Other(Main mc){
        //HERE IT IS!
        Display.getDisplay(mc).setCurrent(this);
        //If I comment out the above, I get no error.

    }
    protected void paint(Graphics g) {
         //Nothing yet
    }

}

, : .

+3

 public Other othr = new Other(this);
 public Display display = Display.getDisplay(this);

public Other othr;
public Display display;

public Main()
{
  othr = new Other(this);
  display = Display.getDisplay(this)
}
0

All Articles