package veda.software.game;
import java.util.ArrayList;
import java.util.Iterator;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class RandomNumGenActivity extends Activity {
View v1 = new View(getApplicationContext());
TextView txtName;
Button submitbutton;
int choice;
Integer level = 1;
String toRem = "";
int life = 0;
int score;
boolean watsUp1 = true;
Handler myHandler = new Handler();
int noOfGames = 0;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.screen2);
txtName = (TextView) findViewById(R.id.editText1);
submitbutton = (Button) findViewById(R.id.button1);
Intent i = getIntent();
choice = Integer.parseInt(i.getStringExtra("choice"));
this.scoreLife();
}
public void scoreLife(){
while(true){
// TODO Auto-generated method stub
if(watsUp1==true){
Context context = getApplicationContext();
CharSequence text = "you have to remember the !!"+"The"+level.toString()+"from last";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.setGravity(Gravity.TOP|Gravity.LEFT, 0, 400);
toast.show();
watsUp1=false;
noOfGames++;
if(noOfGames==5){
noOfGames=0;
level++;
}
if(level==9){
break;
}
else{
RandomNumGenActivity.this.startTheGame();
}
}
}
}
public int getArraySize(){
int arraySize = 0 + (int)(Math.random() * ((9 - 0) + 1));
return arraySize;
}
public void startTheGame(){
ArrayList<Integer> arrayOfNum = new ArrayList<Integer>();
int arraySize = this.getArraySize();
if(arraySize<level&&arraySize<4){
arraySize = this.getArraySize();
}
else{
for (int idx = 1; idx <= arraySize; ++idx){
int randomInt = choice*1 + (int)(Math.random() * ((choice*9 - choice*1) + 1));
arrayOfNum.add(randomInt);
Log.d("randomInt are:",""+randomInt);
}
this.theGame(arrayOfNum);
}
}
public void theGame(ArrayList<Integer> theArray){
Iterator<Integer> ite = theArray.iterator();
int counter = 0;
while(ite.hasNext()){
counter++;
String temp = ite.next().toString();
if(counter == theArray.size()-level){
toRem = temp.toString();
}
Log.d("random array elements are:",""+temp);
showToast(temp);
}
}
public void showToast(String txtToDisplay){
Log.d("inside showToast",""+txtToDisplay);
Context context = getApplicationContext();
int duration = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(context, txtToDisplay, duration);
toast.show();
}
public void buttonClick(View v1){
final Handler handler = new Handler();
final Runnable updater = new Runnable() {
public void run() {
String req = txtName.getText().toString();
System.out.println("req is"+req );
if(req.equals(toRem)){
Context context = getApplicationContext();
CharSequence text = "yo!! you got it right buddy";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
watsUp1=true;
}
else{
Context context = getApplicationContext();
CharSequence text = "you are fired!!";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
watsUp1=true;
}
}
};
Thread x = new Thread() {
public void run() {
handler.post(updater);
}
};
}
}
*
This code is not working.I am a beginner to android and I think this should have worked.Please help..I am not getting an errors.My screen just goes black.
Where exactly is the problem in this code???
The fact that you’re using an infinite loop might be the problem. In fact I can’t think of many cases when infinite loops aren’t problems.
Anyway it appears that your loop iterates once through the first if block of
and then in that block you set watsUp1 = false, which wouldn’t be a problem except you’re in the infinite loop.
Also I would just work on making your code more aesthetically pleasing in the future.