We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

calling alert from 1st class.

redcoder
redcoder asked
on
Medium Priority
289 Views
Last Modified: 2012-08-13
I want to call an alert from another class.
Let say I have Answer.java .:

public void commandAction(Command c, Displayable d)
{
            
if (c == nextCommand)
        {
              if(gameOver == true)
              {                    
                    Display.getDisplay(Millionaire.instance).setCurrent(new AlertExample());
              }  
              else
              Display.getDisplay(Millionaire.instance).setCurrent(new PlayScreen(nextQuest));
            
        }
       
      }


and AlertExample.java:

import javax.microedition.lcdui.*;
import java.io.IOException;

public class AlertExample extends Form implements CommandListener {
private Display display;
   
public AlertExample() {
try
{
         
 Alert myalert = new Alert("== Alert Example ==",  "Game Over.",  null, AlertType.INFO);
   myalert.setTimeout(5000); // milliseconds
  display.setCurrent(myalert);      
}  
catch (Exception e)
}
e.printStackTrace();
}
}

}


Are they valid ?
Comment
Watch Question

Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
try:

mport javax.microedition.lcdui.*;
import java.io.IOException;

public class AlertExample {
   
public static void ShowAlert(Display display) {
try
{
       
 Alert myalert = new Alert("== Alert Example ==",  "Game Over.",  null, AlertType.INFO);
   myalert.setTimeout(5000); // milliseconds
  display.setCurrent(myalert);    
}  
catch (Exception e)
}
e.printStackTrace();
}
}

}

and call it using:

AlertExample.ShowAlert(display);
Leo EikelmanDirector, IT and Business Development

Commented:
It looks valid to me, considering I can't see the rest of the code


What is the problem you are having?

Author

Commented:
Do I need to put extends and implements ?

Author

Commented:
You mean replace

Display.getDisplay(Millionaire.instance).setCurrent(new AlertExample());

with

AlertExample.ShowAlert(display); ?
Leo EikelmanDirector, IT and Business Development

Commented:
objects, he is using the AlertExampel class in this line

Display.getDisplay(Millionaire.instance).setCurrent(new AlertExample());

So I am asuming he is expecting an AlertExample type as a parementer for the setCurrent method.  From which he then gets the message.


What is the problem you are actually having?
Java Developer
CERTIFIED EXPERT
Top Expert 2010
Commented:
yes I missed that. In that case pass the display

import javax.microedition.lcdui.*;
import java.io.IOException;

public class AlertExample extends Form implements CommandListener {
   
public AlertExample(Display display) {
try
{
       
 Alert myalert = new Alert("== Alert Example ==",  "Game Over.",  null, AlertType.INFO);
   myalert.setTimeout(5000); // milliseconds
  display.setCurrent(myalert);    
}  
catch (Exception e)
}
e.printStackTrace();
}
}

}

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
I got this error :

:\WTK22\apps\Millionaire(2)\src\AlertExample.java:8: AlertExample is not abstract and does not override abstract method commandAction(javax.microedition.lcdui.Command,javax.microedition.lcdui.Displayable) in javax.microedition.lcdui.CommandListener
public class AlertExample extends Form implements CommandListener {
       ^
C:\WTK22\apps\Millionaire(2)\src\AlertExample.java:10: cannot resolve symbol
symbol  : constructor Form ()
location: class javax.microedition.lcdui.Form
public AlertExample(Display display) {
                                     ^
C:\WTK22\apps\Millionaire(2)\src\AnswerScreen.java:90: cannot resolve symbol
symbol  : variable display
location: class AnswerScreen
                    AlertExample.ShowAlert(display);
                       
Leo EikelmanDirector, IT and Business Development
Commented:
Ok.

in your Answer.java class could you not just create a new instance of this class?

public void commandAction(Command c, Displayable d)
{

AlertExample alertEx = new AlertExample();
         
if (c == nextCommand)
        {
             if(gameOver == true)
             {                  
                  Display.getDisplay(Millionaire.instance).setCurrent(alertEx);
             }  
             else
             Display.getDisplay(Millionaire.instance).setCurrent(new PlayScreen(nextQuest));
         
        }
       
     }

try that


Leo

Author

Commented:
Get this error :

C:\WTK22\apps\Millionaire(2)\src\AlertExample.java:8: AlertExample is not abstract and does not override abstract method commandAction(javax.microedition.lcdui.Command,javax.microedition.lcdui.Displayable) in javax.microedition.lcdui.CommandListener
public class AlertExample extends Form implements CommandListener {
       ^
C:\WTK22\apps\Millionaire(2)\src\AlertExample.java:12: cannot resolve symbol
symbol  : constructor Form ()
location: class javax.microedition.lcdui.Form
public AlertExample() {
                      ^
Leo EikelmanDirector, IT and Business Development

Commented:
Oh wait,

your AlertExample implements CommandListener

You did not post a method or methods satisfying all of the methods in the CommandListener class.


hope that helps,

Leo

Author

Commented:
Yes,, my AlertExample Is :
import javax.microedition.lcdui.*;
import java.io.IOException;

public class AlertExample extends Form implements CommandListener {
 
private Display display;
   
public AlertExample() {
try
{
       
 Alert myalert = new Alert("== Alert Example ==",  "Game Over.",  null, AlertType.INFO);
   myalert.setTimeout(5000); // milliseconds
  display.setCurrent(myalert);    
}  
catch (Exception e)
{

e.printStackTrace();
}
}

}
Leo EikelmanDirector, IT and Business Development
Commented:
Take a look at this class that implement CommandListner

class FirstForm implements CommandListener {
 
  MIDlet theMIDlet;
 
  Command a = new Command("A",Command.SCREEN,1);
  Command b = new Command("B",Command.SCREEN,1);
 
  FirstForm(MIDlet theMIDlet) {
    super("Form 1");
    this.theMIDlet = theMIDlet;
    addCommand(a);
    addCommand(b);
    setCommandListener(this);
  }
 
  public void commandAction(Command c, Displayable d) {
    if (c == a) {
      SecondForm f = new SecondForm(theMIDlet,0);
      Display.getDisplay(theMIDlet).setCurrent(f);
    }
    else if (c == b) {
      SecondForm f = new SecondForm(theMIDlet,1);
      Display.getDisplay(theMIDlet).setCurrent(f);
    }
  }
}


You need to have commandAction as a method void not as a constructor to satisfy your implementation of CommandListener


Leo
Leo EikelmanDirector, IT and Business Development
Commented:
Notice the

public void commandAction(Command c, Displayable d)

This needs to be IN THE CLASS that is implementing CommandListener

so in your AlertExample class you would need this method in order for it to be able to implement CommandListener

Hope that helps,

Leo

Author

Commented:
Yes, I already add in :

public void commandAction(Command c, Displayable d)
{
      if(c == exitCommand)
      {
            Display.getDisplay(Millionaire.instance).setCurrent(Millionaire.instance.getForm());
      }
       
}


and declaring all necessary variable, but still receiving error :

C:\WTK22\apps\Millionaire(2)\src\AlertExample.java:15: cannot resolve symbol
symbol  : constructor Form ()
location: class javax.microedition.lcdui.Form
public AlertExample() {
                      ^

Author

Commented:
OK I got the program compiled by adding -- super("");
, but having running error :

java.lang.NullPointerException
      at AlertExample.<init>(+63)
      at AnswerScreen.commandAction(+7)
      at javax.microedition.lcdui.Display$DisplayAccessor.commandAction(+282)
      at javax.microedition.lcdui.Display$DisplayManagerImpl.commandAction(+10)
      at com.sun.midp.lcdui.DefaultEventHandler.commandEvent(+68)
      at com.sun.midp.lcdui.AutomatedEventHandler.commandEvent(+47)
      at com.sun.midp.lcdui.DefaultEventHandler$QueuedEventHandler.run(+250)
What is this error mean ?

The program is running but background running (ToolKit) is giving this error ...
Leo EikelmanDirector, IT and Business Development

Commented:
which line is this happening at?

Author

Commented:
I am looking at the execution sequence.. It could be that my execution sequence have problem..
Anyway, I will open a new topic if I couldn't solve it by then..

Author

Commented:
Why is the alert is not showing ?

Author

Commented:
It is not showing when it is triggered.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.