Toast message from Java class

393 Views Asked by At

I'm developing a card game. In the main class of the game called "Mesa" where it fires the main loop of the game I want to Toast some message to see if it works and the next step I want to do if this runs is build the User Interface for this game. All this message I want to show, are Logged sing Log.d(... , ... ) and it works fine. The problem I have is that it seems that screen turns black when trying to Toast these messages. Here's my class for Toast messages:

public class ToastMsg
{

    Context ctx;

    public ToastMsg (Context context)
    {
        this.ctx = context;
    }

    public void makeText(final String text)
    {
           Handler handler = new Handler();        
            handler.post(new Runnable() {
                public void run() {
                    Toast toast = Toast.makeText(ctx, text, Toast.LENGTH_SHORT);
                    toast.show();
                }
            });
    }
}

Here is my onCreate Method from Principal.class where I create a new "Mesa"

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_principal);
    mesa = new Mesa(Principal.this, 3, 1, 3);
    mesa.añadirJugador(new JugadorBot("Jose"));
    mesa.añadirJugador(new JugadorBot("Hasan"));
    mesa.añadirJugador(new JugadorBot("Abdul"));

    runOnUiThread(new Runnable() {

        @Override
        public void run() {
            // TODO Auto-generated method stub
            try {
                mesa.empezarPartida();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                Log.d("ERROR", e.getMessage());
            }
        }
    });

}

In the logCat i get an Error:

06-21 11:55:05.558: W/ActivityManager(1273): Launch timeout has expired, giving up wake lock!
06-21 11:55:05.848: E/WindowManager(1273): Starting window AppWindowToken{b3307548 token=Token{b333f7a8 ActivityRecord{b3398748 u0 com.games.sardineta/.Principal t3}}} timed out

EDIT:

Here's for example when Toastmessage method is used: (This is a method form a class representing a Deck)

public void repartir (int cartas, Jugada jugada)
{
    /* Métedo que recibe el número de cartas a repartir
     *  y los jugadores. Reparte cartas a los jugadores
     *  uno a uno.
     */
    ArrayList<Jugador> jugadores = jugada.mesa.jugadores;
    for (int i=1; i<=cartas; i++)
    {
        for (Jugador jugador : jugadores)
        {
            try
            {
                Carta carta = baraja.get(0);
                jugador.getMano().add(carta);
                baraja.remove(0);
                Log.d("DEBUG", jugador.getNombre() + " recibe " + carta.toString());
                jugada.mesa.toast.makeText(jugador.getNombre() + " recibe " + carta.toString());
                sleep(1000);
            }
            catch (NullPointerException npe)
            {
                throw new Error ("Baraja vacía.");
            }

        }
    }
}

EDIT 2:

public class Mesa {

Principal ctx;
public ToastMsg toast;
......
public Mesa (Context ctx, int numeroJugadores, int numBarajas, int numCartasRepartir)
{
    if (numeroJugadores < 2 || numeroJugadores > NUMERO_MAX_DE_JUGADORES)
        throw new Error ("Número de jugadores no válido.");
    this.ctx = (Principal) ctx;
    toast = new ToastMsg(ctx.getApplicationContext());
     ....
 }}

Any ideas? Some help would be appreciate.

0

There are 0 best solutions below