• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 268
  • Last Modified:

calling alert from 1st class.

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 ?
0
redcoder
Asked:
redcoder
  • 10
  • 7
  • 2
4 Solutions
 
objectsCommented:
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);
0
 
Leo EikelmanCommented:
It looks valid to me, considering I can't see the rest of the code


What is the problem you are having?
0
 
redcoderAuthor Commented:
Do I need to put extends and implements ?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
redcoderAuthor Commented:
You mean replace

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

with

AlertExample.ShowAlert(display); ?
0
 
Leo EikelmanCommented:
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?
0
 
objectsCommented:
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();
}
}

}
0
 
redcoderAuthor 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);
                       
0
 
Leo EikelmanCommented:
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
0
 
redcoderAuthor 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() {
                      ^
0
 
Leo EikelmanCommented:
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
0
 
redcoderAuthor 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();
}
}

}
0
 
Leo EikelmanCommented:
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
0
 
Leo EikelmanCommented:
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
0
 
redcoderAuthor 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() {
                      ^

0
 
redcoderAuthor 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 ...
0
 
Leo EikelmanCommented:
which line is this happening at?

0
 
redcoderAuthor 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..
0
 
redcoderAuthor Commented:
Why is the alert is not showing ?
0
 
redcoderAuthor Commented:
It is not showing when it is triggered.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 10
  • 7
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now