Java global int - java

In an Android Studio app, I have following code:
boolean clicked = true;
Button btnumb1 = (Button) this.findViewById(R.id.numb1);
Button btnumb2 = (Button) this.findViewById(R.id.numb2);
Button btnumb3 = (Button) this.findViewById(R.id.numb3);
Button btnumb4 = (Button) this.findViewById(R.id.numb4);
btnumb1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
clicked = false;
}
});
Button Start = (Button) this.findViewById(R.id.Start);
Start.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (clicked == false) {
Random rand = new Random();
int n = rand.nextInt(4) + 1;
TextView textView = (TextView) findViewById(R.id.randOutput);
textView.setText("" + n);
}
else{
Toast.makeText(getApplicationContext(), "You have to choose a number", Toast.LENGTH_LONG).show();
}
}
});
The idea is that when one of the 4 buttons is clicked, the int clicked is set to 1 and so the final button can only be clicked when it is 1.
But the code doesn't work like this; int clicked = 0; can't be accessed in other public void.
If one of the numbers 1,2,3,4 is clicked then final button can be clicked

The fix: Put public static boolean clicked = false;
under your public class YourClassName { line.
Reasoning: You need to learn how to scope your variables properly. You declared boolean clicked inside the onCreate() function, so the variable is gone after onCreate() is done running.
You should have put the clicked variable inside the class scope level, via public boolean clicked or public static boolean clicked so that even after the function returns, the value is saved.
I highly recommend a beginners java course or textbook before continuing on with your project(s).

Java does not have any concept of global variables. There is either an instance variable or a class variable. To define Global Variable you can make use of static Keyword.
static boolean clicked = true;
Button btnumb1 = (Button) this.findViewById(R.id.numb1);
Button btnumb2 = (Button) this.findViewById(R.id.numb2);
Button btnumb3 = (Button) this.findViewById(R.id.numb3);
Button btnumb4 = (Button) this.findViewById(R.id.numb4);
btnumb1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
clicked = false;
}
});
Button Start = (Button) this.findViewById(R.id.Start);
Start.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (clicked == false) {
Random rand = new Random();
int n = rand.nextInt(4) + 1;
TextView textView = (TextView) findViewById(R.id.randOutput);
textView.setText("" + n);
}
else{
Toast.makeText(getApplicationContext(), "You have to choose a number", Toast.LENGTH_LONG).show();
}
}
});

Yes the variable int clicked = 0; has to be declared final before it is accessible in the other public void functions.
Try this declared a class say ClassClicked
public class ClassClicked {
int clicked;
public int getClicked() {
return clicked;
}
public void setClicked(int clicked) {
this.clicked = clicked;
}
}
Then changed
int clicked = 0;
to
final ClassClicked clicked = new ClassClicked();
clicked.setClicked(0);
And
clicked = 1;
to
clicked.setClicked(1);
Finally
clicked == 1
to
clicked.getClicked() == 1

Related

Remove space between characters

I am developing a simple calculator using Android Studio.
I have built many buttons that emulate the real keys.
I have set a number for each button.
When hitting the button, can get the text from the button pressed as well as print it to the display area where all numbers will appear.
Everything works well except for the fact that a space appears between numbers when being displayed as shown in the emulator.
What can I do ?
static void getSetText (Button button) {
String input;
button.setOnClickListener (new View.OnClickListener() {
#Override
public void onClick (View v) {
Button b = (Button) v;
buttText = b.getText().toString();
if (init) {
display.setText ("");
init = false;
}
display.setText (display.getText() + buttText);
}
});
}
EMULATOR:
A simple trimming would do if the cause of issue is an space,
static void getSetText (Button button) {
String input;
button.setOnClickListener (new View.OnClickListener() {
#Override
public void onClick (View v) {
Button b = (Button) v;
buttText = b.getText().toString();
if (init) {
display.setText ("");
init = false;
}
display.setText (display.getText().toString().trim() + buttText.trim());
}
});
}
But it could also be because of android:letterSpacing in your xml layout. Take a look here regarding this, https://developer.android.com/reference/android/widget/TextView#attr_android:letterSpacing
Remove the blankspace from the String.
static void getSetText (Button button) {
String input;
button.setOnClickListener (new View.OnClickListener() {
#Override
public void onClick (View v) {
Button b = (Button) v;
buttText = b.getText().toString();
if (init) {
display.setText ("");
init = false;
}
display.setText (display.getText().toString().replaceAll(" ","") + buttText.replaceAll(" ",""));
}
});
}

Quiz app not showing dialog in last question

So when the last question is asked on the quiz, it doesn't show the dialog box if it is correct or not. And by the results screen the last question is always considered as 'correct'
public class MainGameActivity extends AppCompatActivity {
FButton buttonA, buttonB, buttonC, buttonD;
Button pause;
TextView questionText, triviaQuizText, timeText, resultText, coinText;
TriviaQuizHelper triviaQuizHelper;
TriviaQuestion currentQuestion;
MediaPlayer mysong;
List<TriviaQuestion> list;
int qid = 0;
int timeValue = 20;
int coinValue = 0;
CountDownTimer countDownTimer;
Typeface tb, sb;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game_main);
mysong = MediaPlayer.create(MainGameActivity.this,R.raw.hs);
mysong.start();
int media_length;
//Initializing variables
questionText = (TextView) findViewById(R.id.triviaQuestion);
pause = (Button) findViewById(R.id.pause);
buttonA = (FButton) findViewById(R.id.buttonA);
buttonB = (FButton) findViewById(R.id.buttonB);
buttonC = (FButton) findViewById(R.id.buttonC);
buttonD = (FButton) findViewById(R.id.buttonD);
triviaQuizText = (TextView) findViewById(R.id.triviaQuizText);
timeText = (TextView) findViewById(R.id.timeText);
resultText = (TextView) findViewById(R.id.resultText);
coinText = (TextView) findViewById(R.id.coinText);
pause.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
pausedialog();
}
});
//Setting typefaces for textview and buttons - this will give stylish fonts on textview and button etc
tb = Typeface.createFromAsset(getAssets(), "fonts/TitilliumWeb-Bold.ttf");
sb = Typeface.createFromAsset(getAssets(), "fonts/shablagooital.ttf");
triviaQuizText.setTypeface(sb);
questionText.setTypeface(tb);
buttonA.setTypeface(tb);
buttonB.setTypeface(tb);
buttonC.setTypeface(tb);
buttonD.setTypeface(tb);
timeText.setTypeface(tb);
resultText.setTypeface(sb);
coinText.setTypeface(tb);
//Our database helper class
triviaQuizHelper = new TriviaQuizHelper(this);
//Make db writable
triviaQuizHelper.getWritableDatabase();
//It will check if the ques,options are already added in table or not
//If they are not added then the getAllOfTheQuestions() will return a list of size zero
if (triviaQuizHelper.getAllOfTheQuestions().size() == 0) {
//If not added then add the ques,options in table
triviaQuizHelper.allQuestion();
}
//This will return us a list of data type TriviaQuestion
list = triviaQuizHelper.getAllOfTheQuestions();
//Now we gonna shuffle the elements of the list so that we will get questions randomly
Collections.shuffle(list);
//currentQuestion will hold the que, 4 option and ans for particular id
currentQuestion = list.get(qid);
//countDownTimer
countDownTimer = new CountDownTimer(22000, 1000) {
public void onTick(long millisUntilFinished) {
//here you can have your logic to set text to timeText
timeText.setText(String.valueOf(timeValue) + "\"");
//With each iteration decrement the time by 1 sec
timeValue -= 1;
//This means the user is out of time so onFinished will called after this iteration
if (timeValue == -1) {
//Since user is out of time setText as time up
// resultText.setText(getString(R.string.timeup));
timeUp();
//Since user is out of time he won't be able to click any buttons
//therefore we will disable all four options buttons using this method
// disableButton();
}
}
//Now user is out of time
public void onFinish() {
//We will navigate him to the time up activity using below method
//timeUp();
}
}.start();
//This method will set the que and four options
updateQueAndOptions();
}
public void updateQueAndOptions() {
//This method will setText for que and options
questionText.setText(currentQuestion.getQuestion());
buttonA.setText(currentQuestion.getOptA());
buttonB.setText(currentQuestion.getOptB());
buttonC.setText(currentQuestion.getOptC());
buttonD.setText(currentQuestion.getOptD());
timeValue = 20;
//Now since the user has ans correct just reset timer back for another que- by cancel and start
countDownTimer.cancel();
countDownTimer.start();
//set the value of coin text
coinText.setText(String.valueOf(coinValue));
//Now since user has ans correct increment the coinvalue
coinValue++;
}
public void buttonA(View view) {
Button button = (Button) view;
buttonPressed(button);
}
public void buttonB(View view) {
Button button = (Button) view;
buttonPressed(button);
}
public void buttonC(View view) {
Button button = (Button) view;
buttonPressed(button);
}
public void buttonD(View view) {
Button button = (Button) view;
buttonPressed(button);
}
public void buttonPressed(Button button) {
if (qid < list.size() - 1) {
disableButton();
if (button.getText().equals(currentQuestion.getAnswer())) {
correctDialog();
} else {
incorrectDialog();
coinValue--;
}
} else {
gameWon();
}
}
//This method will navigate from current activity to GameWon
public void gameWon() {
Intent intent = new Intent(this, GameWon.class);
intent.putExtra("score", coinValue);
startActivity(intent);
finish();
}
//This method is called when user ans is wrong
//this method will navigate user to the activity PlayAgain
public void gameLostPlayAgain() {
Intent intent = new Intent(this, PlayAgain.class);
startActivity(intent);
finish();
}
//This method is called when time is up
//this method will navigate user to the activity Time_Up
public void timeUp() {
Intent intent = new Intent(this, Time_Up.class);
startActivity(intent);
finish();
}
//If user press home button and come in the game from memory then this
//method will continue the timer from the previous time it left
#Override
protected void onRestart() {
super.onRestart();
mysong.getCurrentPosition();
mysong.start();
countDownTimer.start();
}
//When activity is destroyed then this will cancel the timer
#Override
protected void onStop() {
super.onStop();
countDownTimer.cancel();
}
//This will pause the time/
#Override
protected void onPause() {
super.onPause();
mysong.pause();
countDownTimer.cancel();
}
//On BackPressed
#Override
public void onBackPressed() {
Intent intent = new Intent(this, HomeScreen.class);
startActivity(intent);
finish();
}
//This dialog is show to the user after he ans correct
public void correctDialog() {
final Dialog dialogCorrect = new Dialog(MainGameActivity.this);
dialogCorrect.requestWindowFeature(Window.FEATURE_NO_TITLE);
if (dialogCorrect.getWindow() != null) {
ColorDrawable colorDrawable = new ColorDrawable(Color.TRANSPARENT);
dialogCorrect.getWindow().setBackgroundDrawable(colorDrawable);
}
dialogCorrect.setContentView(R.layout.dialog_correct);
dialogCorrect.setCancelable(false);
dialogCorrect.show();
//Since the dialog is show to user just pause the timer in background
onPause();
onRestart();
TextView correctText = (TextView) dialogCorrect.findViewById(R.id.correctText);
FButton buttonNext = (FButton) dialogCorrect.findViewById(R.id.dialogNext);
//Setting type faces
correctText.setTypeface(sb);
buttonNext.setTypeface(sb);
//OnCLick listener to go next que
buttonNext.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//This will dismiss the dialog
dialogCorrect.dismiss();
//it will increment the question number
qid++;
//get the que and 4 option and store in the currentQuestion
currentQuestion = list.get(qid);
//Now this method will set the new que and 4 options
updateQueAndOptions();
//reset the color of buttons back to white
resetColor();
//Enable button - remember we had disable them when user ans was correct in there particular button methods
enableButton();
}
});
}
//INCORRECT//////////////////////////////////////////////////////////////
public void incorrectDialog() {
final Dialog incorrectDialog = new Dialog(MainGameActivity.this);
incorrectDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
if (incorrectDialog.getWindow() != null) {
ColorDrawable colorDrawable = new ColorDrawable(Color.TRANSPARENT);
incorrectDialog.getWindow().setBackgroundDrawable(colorDrawable);
}
incorrectDialog.setContentView(R.layout.dialog_incorrect);
incorrectDialog.setCancelable(false);
incorrectDialog.show();
//Since the dialog is show to user just pause the timer in background
onPause();
onRestart();
TextView IncorrectText = (TextView) incorrectDialog.findViewById(R.id.IncorrectText);
FButton buttonNext = (FButton) incorrectDialog.findViewById(R.id.dialogNext);
//Setting type faces
IncorrectText.setTypeface(sb);
buttonNext.setTypeface(sb);
//OnCLick listener to go next que
buttonNext.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//This will dismiss the dialog
incorrectDialog.dismiss();
//it will increment the question number
qid++;
//get the que and 4 option and store in the currentQuestion
currentQuestion = list.get(qid);
//Now this method will set the new que and 4 options
updateQueAndOptions();
//reset the color of buttons back to white
resetColor();
//Enable button - remember we had disable them when user ans was correct in there particular button methods
enableButton();
}
});
}
//This method will make button color white again since our one button color was turned green
public void resetColor() {
buttonA.setButtonColor(ContextCompat.getColor(getApplicationContext(),R.color.white));
buttonB.setButtonColor(ContextCompat.getColor(getApplicationContext(),R.color.white));
buttonC.setButtonColor(ContextCompat.getColor(getApplicationContext(),R.color.white));
buttonD.setButtonColor(ContextCompat.getColor(getApplicationContext(),R.color.white));
}
//This method will disable all the option button
public void disableButton() {
buttonA.setEnabled(false);
buttonB.setEnabled(false);
buttonC.setEnabled(false);
buttonD.setEnabled(false);
}
//This method will all enable the option buttons
public void enableButton() {
buttonA.setEnabled(true);
buttonB.setEnabled(true);
buttonC.setEnabled(true);
buttonD.setEnabled(true);
}
public void pausedialog()
{
final Dialog pausedialog = new Dialog(MainGameActivity.this);
pausedialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
if (pausedialog.getWindow() != null) {
ColorDrawable colorDrawable = new ColorDrawable(Color.TRANSPARENT);
pausedialog.getWindow().setBackgroundDrawable(colorDrawable);
}
pausedialog.setContentView(R.layout.activity_dialog_pause);
pausedialog.setCancelable(false);
pausedialog.show();
//Since the dialog is show to user just pause the timer in background
onPause();
Button resume = (FButton) pausedialog.findViewById(R.id.resume);
Button menu = (FButton) pausedialog.findViewById(R.id.menu);
Button music = (FButton) pausedialog.findViewById(R.id.music);
resume.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//This will dismiss the dialog
pausedialog.dismiss();
onRestart();
}
});
menu.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent2 = new Intent(MainGameActivity.this,HomeScreen.class);
// mysong.release();
// finish();
startActivity(intent2);
finish();
}
});
}
}
EDIT: posted the full code. I tried understanding it again but I don't know what to do anymore this may be the only problem I have left other than inserting images (that's another problem for now I wanna finish this)
EDIT: I tried fixing the if statements like this:
if (button.getText().equals(currentQuestion.getAnswer())) {
correctDialog();
} else {
incorrectDialog();
coinValue--;
}
if (qid < list.size() - 1) {
disableButton();
} else
{
gameWon();
}
It works now, the last answer is now also registered if right or wrong BUT the dialog box flashes for a split second and automatically goes to the results screen.

What am i doing wrong at my simple android app?

I am trying to make a app that has a switch a button and a text and if you turn the switch on and press the button; the number displayed on the text will be added by 1. But if the switch is turned off the number will be subtracted by 1.
but when i run my app and press the button, the app crashes...
i do not have much experience at programming and i do not know what im doing wrong. and i have only tried this code.
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView text = (TextView)findViewById(R.id.textView);
final Button button = (Button)findViewById(R.id.button);
Switch mySwitch = (Switch)findViewById(R.id.mySwitch);
mySwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked== true){
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String text_string = text.getText().toString();
int text_int = Integer.parseInt(text_string);
text_int++;
text.setText(text_int);
}
});
}
if (isChecked == false) {
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String text_string = text.getText().toString();
int text_int = Integer.parseInt(text_string);
text_int++;
text.setText(text_int);
}
});
}
}
});
}
}
so this should behave as i described earlier but it doesn't.
Your app crashes because you are trying to set an int to a textview.setText()and when you pass an int to this method it expects it to be a resource id and which could not be found in your case that's why it will throw ResourceNotFoundException and crashes.
You should set text as following:
text.setText(String.valueOf(text_int));
You’re nesting listeners but that logic doesn’t work sequentially. You should declare your listeners separately. I suggest you create a boolean that holds the state of the switch and one button listener. Within the listener check if switch is enabled then run your calculations and do the same if the switch is disabled.
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(mySwitch.isChecked(){
String text_string = text.getText().toString();
int text_int = Integer.parseInt(text_string);
text_int++;
text.setText(String.valueOf(text_int));
} else {
String text_string = text.getText().toString();
int text_int = Integer.parseInt(text_string);
text_int++;
text.setText(String.valueOf(text_int));
}
}
});
You don't need a listener for the Switch, but only 1 listener for the Button:
final TextView text = (TextView)findViewById(R.id.textView);
final Button button = (Button)findViewById(R.id.button);
final Switch mySwitch = (Switch)findViewById(R.id.mySwitch);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String text_string = text.getText().toString();
int text_int = 0;
try {
text_int = Integer.parseInt(text_string);
} catch (NumberFormatException e) {
e.printStackTrace();
}
if (mySwitch.isChecked())
text_int++;
else
text_int--;
text.setText("" + text_int);
}
});
Every time you click the Button, in its listener the value in the TextView is increased or decreased depending on whether the Switch is checked or not.

Changing value of variable in android /JAVA

I was trying to make a simple app in which clicking tapb Button increments the variable value of notaps and the reset Button sets it to 0. When I click on tapb it increments the value & clicking reset resets it but when I again click tabp it increments from the previous value.
Eg :
init value of notaps = 0;
I click tabp 3 times and notaps value = 3
I click reset and notaps value = 0
I click tabp 3 times and notaps value = 4
Button tapb = (Button)findViewById(R.id.tapb);
Button reset = (Button)findViewById(R.id.reset);
tapb.setOnClickListener(
new Button.OnClickListener(){
int notaps = 0;
#Override
public void onClick(View v) {
TextView taps = (TextView)findViewById(R.id.taps);
notaps++;
taps.setText(String.valueOf(notaps));
}
}
);
reset.setOnClickListener(
new Button.OnClickListener() {
#Override
public void onClick(View v) {
TextView taps = (TextView) findViewById(R.id.taps);
int notaps=0;
taps.setText(String.valueOf(notaps));
}
}
);
First, you have 2 instances of int named notaps that have nothing to do with each other. Your reset button does not set the right notaps variable.
Here's a snippet that should work.
private int notaps;
Button tapb = (Button)findViewById(R.id.tapb);
Button reset = (Button)findViewById(R.id.reset);
TextView taps = (TextView)findViewById(R.id.taps);
tapb.setOnClickListener(
new Button.OnClickListener(){
#Override
public void onClick(View v) {
notaps++;
taps.setText(String.valueOf(notaps));
}
}
);
reset.setOnClickListener(
new Button.OnClickListener() {
#Override
public void onClick(View v) {
notaps = 0;
taps.setText(String.valueOf(notaps));
}
}
);
Try declaring your notaps variable globally meaning where every you declare you buttons declare your notaps variable there, here is what I mean.
Button tapb = (Button)findViewById(R.id.tapb);
Button reset = (Button)findViewById(R.id.reset);
int notaps = 0; // declare variable globally
tapb.setOnClickListener(
new Button.OnClickListener(){
#Override
public void onClick(View v) {
TextView taps = (TextView)findViewById(R.id.taps);
taps.setText(String.valueOf(++notaps));
}
}
);
reset.setOnClickListener(
new Button.OnClickListener() {
#Override
public void onClick(View v) {
TextView taps = (TextView) findViewById(R.id.taps);
taps.setText(String.valueOf(notaps=0));
}
}
);

Creating android trivia game , cannot proceed to next quest. after it is correctly answered

1.first a random number is selected.
2.the random number corresponds to a case (trivia question) in a switch statement.
3.the case has an onClick() for every possible answer.
4.when the correct answer (and it's corresponding onClick) is selected
I want the dice to roll again and progress the game to the next question.
5.My debug log says tells me the value of this dice rolled. However, I cannot get the view to change for the corresponding case in the switch.
`public class BeginGame extends Activity {
Random generator = new Random();
private static final String TAG = "MyActivity";
boolean dupe = true;
boolean done = false;
int intitializedQuestionValue = -2;
final int rows = 3;
final int columns = 25;
final int RIGHT = -2000;
final int WRONG = -1000;
int score = 0;
int num;
//define array of three rows and 25 columns
int[][] questionArray = new int [rows][columns];
int[] questionNumber = new int [25];
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
final Button button0 = (Button) findViewById(R.id.button0);
final Button button1 = (Button) findViewById(R.id.button1);
final Button button2 = (Button) findViewById(R.id.button2);
final Button button3 = (Button) findViewById(R.id.button3);
final TextView text = (TextView) findViewById(R.id.TextView01);
//initialize "dice"
for (int i=0; i<25; i++){
questionNumber[i] = -1;
}
for(int i=0; i < columns; i++)
questionArray[0][i] = intitializedQuestionValue;
//set all questions to answered WRONG
for (int i=0; i <columns; i++)
questionArray[1][i] = WRONG;
rollDice();
loop: while (!done)
switch (num) {
case 0:
text.setText("press Virginia0:");
button0.setText("Alabama");
button0.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
Toast.makeText(BeginGame.this, "fail", Toast.LENGTH_SHORT).show(); rollDice();
//questionArray[2][0]= score -2;
}
});
button1.setText("Mississippi");
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Toast.makeText(BeginGame.this, "fail", Toast.LENGTH_SHORT).show(); rollDice();
//questionArray[2][0]= score - 2;
}
});
button2.setText("Philadelphia");
button2.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
Toast.makeText(BeginGame.this, "fail", Toast.LENGTH_SHORT).show(); rollDice();
//questionArray[2][0]= score -2;
}
});
button3.setText("Virginia");
button3.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
Toast.makeText(BeginGame.this, "success", Toast.LENGTH_SHORT).show(); rollDice();
//questionArray[1][0]=RIGHT;
//questionArray[2][0]= score + 5;
}
});
break loop;
case 1:
text.setText("press alabama1:");
button0.setText("Alabama");
button0.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
Toast.makeText(BeginGame.this, "success", Toast.LENGTH_SHORT).show(); rollDice();
}
});
button1.setText("Mississippi");
button1.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
}
});
button2.setText("Philadelphia");
button2.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
}
});
button3.setText("Virginia");
button3.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
}
});
break loop;
`
your program won't work this way. You have to understand more of android.
The onCreate method is called when an activity is created. ie : when it comes to front. It is executed on the UI thread : a thread that interacts with the user and should never be blocked. So, you are basically blocking the UI thread with your loop (does num ever change by the way ?).
What you should do is remove your while loop from the onCreate method. Just use it to initialize your activity, maybe data structures like questions and widgets and their listeners.
And now give more logic to your listeners : when a button is clicked then change your question and refresh the interface so that the new questions display. Do that until there is no more question to ask.
Do never block the UI Thread, make it free so users can use your app.

Categories

Resources