Skip to main content
gamedev.net
🔒 Locked 🎮 Unity

Problems using Command Listener. :(

Started by Goguigo Sep 24, 2006 at 8:45 PM 4 replies 600+ views
Original Post
Goguigo
Goguigo
Hey guys. I'm having some trouble using the command listener. I implemented the following main class:
[sourcecode=javax]
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

public class MainBase extends MIDlet implements Runnable, CommandListener
{
	private Display desplegar;
	private Command comandoSalir;//Comando de Salida
	
	private GameAlfaClass program;
	public int opcion=99;
	public boolean isPlay=true;
	public boolean isOut=true;
	public void startApp() throws MIDletStateChangeException
	{
		desplegar = Display.getDisplay(this);
		program=new GameAlfaClass(this);
		comandoSalir = new Command("Salir",Command.EXIT,1);
		program.addCommand(comandoSalir);
		program.setCommandListener(this);
		Thread t = new Thread(this);
	    t.start();
	}
	public void run()
	{
		try
		{
			program.start();
			desplegar.setCurrent(program);    
			isOut = true;
			while (isOut){}    
			program.stop();
			program = null;

		}
		catch (Exception ex)
		{
			System.out.println(ex);
			notifyDestroyed();
		}

		
	}
	public Display getDisplay()
	{
		return desplegar;  
	}
	public void pauseApp()
	{
	}
	public void destroyApp(boolean unconditional)
	{
		notifyDestroyed();
	}
	public void commandAction(Command c, Displayable s) 
	{
		if (c == comandoSalir) 
		{
			destroyApp(true);
			notifyDestroyed();
		}
	}
}


[/sourcecode]

The GAMEALFACLASS works like this:

public class GameAlfaClass extends GameCanvas implements Runnable


======================

The problem comes when i run this app on my Nokia 6670. When i use the command "comandoSalir", i get this two errors:
1) Buffer closed java.lang.RunTimeException !

2) 0 java.lang.NullPointerException !


Anyone knows something about this?

Thanks
shmoove
shmoove
I don't think it has anything to do with your Command/CommandListener. Looks like something is wrong with your app's exit procedures.

Some comments:
* notifyDestroyed() is getting called at least two and maybe three (if the exception is thrown in the body of your run() method) times.
* comandoSalir isn't stopping that thread (by setting isOut = false).
* If GameAlfaClass is already Runnable, and I assume it's start() method starts it's thread, then there is no real reason to keep the run() method of the MIDlet thread alive. That thread doesn't do anything (you can put the call to program.stop() in GameAlfaClass's thread if it has to be called).

shmoove
Goguigo
Goguigo
so, in program.stop(); i kill the GameAlfaClass' thread? How do i kill it?

Thanks
shmoove
shmoove
What do you mean by "kill" it. If you have any threads running you stop them. You don't really need to set it to null.

I would change run() into:
	public void run()	{		try		{			program.start();			desplegar.setCurrent(program);  		}		catch (Exception ex)		{			System.out.println(ex);			notifyDestroyed();		}			}

and commandAction() into:
	public void commandAction(Command c, Displayable s) 	{		if (c == comandoSalir) 		{                        program.stop();			destroyApp(true);		}	}


This will get rid of that extra thread. I don't know if it will solve the error because that might come from something in GameAlfaClass.

shmoove
Goguigo
Goguigo
Here's the GameAlfaClass:

[source code=javax]import javax.microedition.midlet.*;import javax.microedition.lcdui.*;import javax.microedition.lcdui.game.*;public class GameAlfaClass extends GameCanvas implements Runnable{	private boolean isPlay;//Variable de juego activo	public MainBase padre;//objeto que hace referencia a las variables y 	Graphics g = getGraphics();//obtenemos los gráficos	public GameAlfaClass(MainBase padre)//construimos la clase	{		super(true);//necesario		setFullScreenMode(true);		this.padre = padre;	}		public void start() throws MIDletStateChangeException//automaticamente iniciamos un thread	{		isPlay = true;		Thread t = new Thread(this);		t.start();	}		public void stop() { isPlay = false;}		public void run() 	{        //..	}	// CALCULAMOS EL INPUT DEL USUARIO	private void input() 	{        //..	}	private void drawScreen(Graphics g) //dibujar pantalla	{        //..	}}


Ok... if i understood: i need to kill this thread (t) when exit?

The main class remains the same
Thanks
shmoove
shmoove
Calling program.stop() would do that, wouldn't it (the run() method is empty so I'm guessing)?

shmoove

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.