×

Discussion Board

Results 1 to 6 of 6
  1. #1
    Regular Contributor
    Join Date
    Sep 2009
    Posts
    84

    cant move from one screen to another

    Code:
    import javax.microedition.lcdui.Display;
    import javax.microedition.midlet.*;
    
    public class Midlet extends MIDlet
     {
        Display display;
        Controller c;
    
        public Midlet()
         {
            display = Display.getDisplay(this);
            c = new Controller(this);
         }
    
        public void startApp()
         {
            c.setCurrentScreen(c.getCurrentScreen());
         }
    
        public void pauseApp() {}
    
        public void destroyApp(boolean unconditional)
         {
            notifyDestroyed();
         }
     }

    Code:
    import javax.microedition.lcdui.Display;
    
    public class Controller
     {
        Midlet m;
        Display display;
        int CurrentScreen = 1;
    
        public Controller(Midlet m)
         {
            this.m = m;
            display = m.display;
         }
    
        public int getCurrentScreen()
         {
            return CurrentScreen;
         }
    
        public void setCurrentScreen(int screenNumber)
         {
            if (screenNumber == 1)
             {
                display.setCurrent(new WelcomeScreen());
             }
         }
    
        public void checkIfFirstTime()
         {
            System.out.println("so far so good");
         }
     }

    Code:
    import java.util.Timer;
    import java.util.TimerTask;
    import javax.microedition.lcdui.Form;
    
    public class WelcomeScreen extends Form
     {
        Timer timer;
        TestTimerTask task;
    
        public WelcomeScreen()
         {
            super(null);
            append("Welcome");
            timer = new Timer();
            task = new TestTimerTask();
            timer.schedule(task,1000);
         }
      }
    
    class TestTimerTask extends TimerTask
     {
        Controller c;
    
        public final void run()
         {
            c.checkIfFirstTime();
         }
     }
    Apologies for the many lines. Why won't run method go to checkIfFirstTime() method in controller class? i put a print statement above it just to confirm that run is being accessed. the purpose of this app is to first show a welcome screen to the user then it performs a function or two then it goes to another screen. or to sum up, move from one screen to another using a timer. what am i doing wrong?

    Thank you.

  2. #2
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: cant move from one screen to another

    You never initialize "c" in TestTimerTask?

    (Oh, I'd recommend you declare your member variables as "private". They aren't private by default.)

    Graham.

  3. #3
    Regular Contributor
    Join Date
    Sep 2009
    Posts
    84

    Re: cant move from one screen to another

    1- i should do this for every class i have in this app? making the vars private

    2- here are the new classes. fixed. passed the instance of controller around so that id still hve one instance of it at all times instead of initializing it and having more than one instance. thank you it's fixed now

    Code:
    import java.util.Timer;
    import java.util.TimerTask;
    import javax.microedition.lcdui.Form;
    
    public class WelcomeScreen extends Form
     {
        Timer timer;
        TestTimerTask task;
        Controller c;
    
        public WelcomeScreen(Controller c)
         {
            super(null);
            this.c = c;
            append("Welcome");
            timer = new Timer();
            task = new TestTimerTask(this.c);
            timer.schedule(task,1000);
         }
      }
    
    class TestTimerTask extends TimerTask
     {
        Controller c;
    
        public TestTimerTask (Controller c)
         {
            this.c = c;
         }
    
        public final void run()
         {
            c.checkIfFirstTime();
         }
     }

    Code:
    import javax.microedition.lcdui.Display;
    
    public class Controller
     {
        Midlet m;
        Display display;
        int CurrentScreen = 1;
        static Controller ref;
    
        public Controller(Midlet m)
         {
            this.m = m;
            display = m.display;
            ref = this;
         }
    
        public int getCurrentScreen()
         {
            return CurrentScreen;
         }
    
        public void setCurrentScreen(int screenNumber)
         {
            if (screenNumber == 1)
             {
                display.setCurrent(new WelcomeScreen(ref));
             }
         }
    
        public void checkIfFirstTime()
         {
            System.out.println("so far so good");
         }
    }]

  4. #4
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: cant move from one screen to another

    Well... up to you. As a general rule when writing Java (or any OO language), you want to make member variables private to ensure encapsulation. Encapsulation is the practice of ensuring that data access only occur through specific methods (such as getters and setters). These methods can ensure that correct validation is applied to any change to data, that concurrent accesses to data are synchronized, and so on. The public methods form the defined interface to the objects, and hide the internal workings of the object from the outside world. Keeping members (variables and, where possible, methods) private, minimizes the number of ways that one object can influence another, keeps the code simple, and so helps to reduce bugs.

    No need to keep a reference to the Controller object in WelcomeScreen, since you never use it (you pass is straight to TestTimerTask). Minimize the number of member variables you have - not only does this help reduce complexity (less data shared between methods), but also reduces the number of references to each object. Any reference to an object will prevent that object from being garbage collected, so could result in your app using more memory than you need.

    Graham.

  5. #5
    Regular Contributor
    Join Date
    Sep 2009
    Posts
    84

    Re: cant move from one screen to another

    Thank you. I'll make sure i follow th eprivate way. As for the reference, in the entire app, only one reference is being passed. the one belonging to controller. im trying to follow the mvc model were one class (or network of classes) are responsible for the operations being carried out. like the management of a company. so assuming i got ur second paragraph right, there are only two references in my prj. the first is midlet and the second is controller. the controller reference gets passed everytime i go to a new screen (which is a class). whenever i need something done, from that class, i call w/econtroller method is needed via that reference. hopefully you'll agree that i am indeed using one instance during hte entire app hence there'sonly that one reference that cant be gc?

  6. #6
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: cant move from one screen to another

    I think you're slightly confused between instance (object) and reference (pointer to object).

    Code:
    // I create one instance of Fish (we'll call this instance "carp"), and store the reference to that instance in "fish1".
    Fish fish1 = new Fish("carp");
    
    // I make a copy of the refernce into "fish2" - I still have one instance, but now two references to it
    Fish fish2 = fish1;
    
    // I create a new instance of Fish (which we'll call "haddock"), and store the reference in "fish1" - now we have two instances
    fish1 = new Fish("haddock");
    // at this point, "fish1" holds a reference to "haddock", not "carp" - but "carp" isn't garbage, because it is still referenced from "fish2".
    In your code, you might have only one Controller instance, but you have multiple references to it. The reference held in WelcomeScreen.c is (currently) redundant, since it is written to and read from only in the same method (use a local variable - in fact, you already have one, the parameter "c").

    Might seem trivial now, but you'll appreciate keeping it simple as your app gets more complicated. MVC can also become a hideous mess if you don't keep the number of object references to a minimum.

    One way of implementing MVC in Java ME is described in the wiki.

    Graham.

Similar Threads

  1. move a background
    By yopyop in forum Symbian
    Replies: 5
    Last Post: 2009-05-19, 13:11
  2. move pict
    By eng.ppu in forum Symbian Media (Closed)
    Replies: 1
    Last Post: 2008-11-27, 23:12
  3. Hinted Move
    By kash_meu in forum Streaming and Video
    Replies: 1
    Last Post: 2008-02-08, 22:08
  4. Replies: 4
    Last Post: 2007-08-31, 07:32
  5. program to move files from phone main screen
    By j2me_raj in forum Symbian
    Replies: 6
    Last Post: 2004-11-19, 10:51

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×