Foros del Web » Programando para Internet » Android »

[SOLUCIONADO] Juego Ahorcado

Estas en el tema de Juego Ahorcado en el foro de Android en Foros del Web. Buenas a todos, estoy aprendiendo a programar en android y tengo un pequeño problema con la realizacion de mi codigo. A la hora de lanzar ...
  #1 (permalink)  
Antiguo 05/06/2013, 13:09
 
Fecha de Ingreso: junio-2013
Mensajes: 2
Antigüedad: 10 años, 9 meses
Puntos: 0
Juego Ahorcado

Buenas a todos, estoy aprendiendo a programar en android y tengo un pequeño problema con la realizacion de mi codigo. A la hora de lanzar los eventos setOnItemClickListener y setOnClickListener me da error y me pide hacer un cast pero si le pongo el cast al lanzar la aplicacion me la lanza y salta error. Les pego mi codigo a ver si me pueden ayudar

Gracias de antemano!


Cita:

package com.example.ahorcado;

import java.util.Random;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.Color;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends Activity {

GridView tablaLetras;
Button boton;
TextView textoRaya;
TextView textoResultado;
TextView fallos;
ImageView imagenes;
String[] Sabecedario= new String[]{"A","B","C","D","E","F","G","H","I","J","K","L"," M","N","Ñ","O","P","Q","R","S","T","U","V","W","X" ,"Y","Z"};
String[] Ssolucion = new String[]{"_","_","_","_","_","_","_","_"};
String[] Spalabras = new String[]{"MARIA","PEDRO","ANTONIO","JUAN","ALVARO","DAVID" ,"RAUL","LAURA","TERESA","MARTA","ROCIO","OLIVIA", "CRISTINA","RUBEN","IRENE","LUCAS","MIGUEL","LUIS" ,"CARLOTA","LUCIA","CARLOS"};
int aleatorio;
boolean error=true;
int numErrores = 0;
int numAciertos=0;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

this.textoRaya=(TextView)findViewById(R.id.textVie w1);
this.textoResultado=(TextView)findViewById(R.id.Me nsajeResultado);
this.fallos=(TextView)findViewById(R.id.NumFallos) ;
this.tablaLetras=(GridView)findViewById(R.id.Tabla Abecedario);

// ArrayAdapter<String> adaptador=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, Sabecedario);

// this.tablaLetras.setAdapter(adaptador);
this.boton=(Button)findViewById(R.id.BEmpezar);
this.imagenes=(ImageView)findViewById(R.id.imagene s);

this.textoRaya.setText("");

//ERRRORES
tablaLetras.setOnItemClickListener(this);
boton.setOnClickListener(this);

jugar();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

public void fotosErrores()
{
if(numErrores==0)
{
this.imagenes.setImageResource(R.drawable.error_0) ;
}
else if(numErrores==1)
{
this.imagenes.setImageResource(R.drawable.error_1) ;
}
else if(numErrores==2)
{
this.imagenes.setImageResource(R.drawable.error_2) ;
}
else if(numErrores==3)
{
this.imagenes.setImageResource(R.drawable.error_3) ;
}
else if(numErrores==4)
{
this.imagenes.setImageResource(R.drawable.error_4) ;
}
else if(numErrores==5)
{
this.imagenes.setImageResource(R.drawable.error_5) ;
}
else if(numErrores==6)
{
this.imagenes.setImageResource(R.drawable.error_6) ;
}
else if(numErrores==7)
{
this.imagenes.setImageResource(R.drawable.error_7) ;
}
else if(numErrores==Cool
{
this.imagenes.setImageResource(R.drawable.error_8) ;
}
else if(numErrores==9)
{
this.imagenes.setImageResource(R.drawable.error_9) ;
}
}

public void jugar()
{
ArrayAdapter<String> adaptador=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, Sabecedario);
tablaLetras.setAdapter(adaptador);

this.numErrores=0;
this.fallos.setText("fallos "+numErrores+"/9");
this.numAciertos=0;
this.Ssolucion=new String[]{"_","_","_","_","_","_","_","_"};
this.tablaLetras.setEnabled(true);
this.tablaLetras.setBackgroundColor(Color.WHITE);
this.textoRaya.setText("");
this.textoResultado.setText("");

Random r = new Random();
this.aleatorio=r.nextInt(19-0);

for(int i=0;i<Spalabras[aleatorio].length();i++)
{
this.textoRaya.setText(textoRaya.getText()+"_ ");
}

fotosErrores();
}

public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
{
arg1.setEnabled(false);
arg1.setBackgroundColor(Color.RED);
String palabraJugando;
this.error=true;
this.textoRaya.setText("");

for(int i=0;i<Spalabras[aleatorio].length();i++)
{
palabraJugando=""+Spalabras[aleatorio].charAt(i);

if(Sabecedario[arg2].equals(palabraJugando))
{
this.Ssolucion[i]=this.Sabecedario[arg2];
this.numAciertos++;
arg1.setBackgroundColor(Color.GREEN);
this.error=false;
}

this.textoRaya.setText(this.textoRaya.getText()+" "+this.Ssolucion[i]);
}

if(numAciertos==Spalabras[aleatorio].length())
{
this.textoResultado.setText("Felicidades eres el ganador!");
this.tablaLetras.setEnabled(false);
}

if(error==true)
{
this.numErrores++;

jugar();

this.fallos.setText("fallos "+numErrores+"/9");

if(numErrores==9)
{
this.textoResultado.setText("Lo siento has perdido");
this.tablaLetras.setEnabled(false);
}
}
}

public void onClick(View arg0)
{
if(arg0.getId()==R.id.BEmpezar)
{
jugar();
}
}

}
  #2 (permalink)  
Antiguo 05/06/2013, 15:07
Avatar de hieloverde  
Fecha de Ingreso: julio-2005
Ubicación: México, D.F
Mensajes: 467
Antigüedad: 18 años, 8 meses
Puntos: 5
Respuesta: Juego Ahorcado

Hola, comenzando te faltara implementar el OnClickListener en tu clase:

Ejemplo:
Código:
...

public class MainActivity extends Activity implements OnClickListener, OnItemClickListener {

...
Para que luego puedas haces override a sus metodos.

Espero te sirva.
__________________
<? echo("1 <script> dice + que 1000 palabras"); ?> EspacioMéxico
  #3 (permalink)  
Antiguo 06/06/2013, 02:30
 
Fecha de Ingreso: junio-2013
Mensajes: 2
Antigüedad: 10 años, 9 meses
Puntos: 0
Respuesta: Juego Ahorcado

Cita:
Iniciado por hieloverde Ver Mensaje
Hola, comenzando te faltara implementar el OnClickListener en tu clase:

Ejemplo:
Código:
...

public class MainActivity extends Activity implements OnClickListener, OnItemClickListener {

...
Para que luego puedas haces override a sus metodos.

Espero te sirva.
Gracias por la ayuda, efectivamente me habia olvidado implementar los listener.

Saludos.

Etiquetas: activity, ahorcado, aplicacion, java, juego, layout, todo
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 04:14.