I am writing a Rock Paper Scissors program in android studio, here's what I have so far: (everything necessary is imported)
Random compans = new Random();
int low = 1;
int high = 3;
int RPS = compans.nextInt(high-low) + low;
public void rock (View view) {
Button Rock = (Button) findViewById(R.id.rock);
Rock.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
/* all capitals = user answer */
if (RPS == 1) {
startActivity(new Intent(MainActivity.this,Tie.class));
//ROCK-rock tie
}
else if (RPS == 2) {
//ROCK-paper lose
startActivity(new Intent(MainActivity.this,Lose.class));
}
else if (RPS == 3) {
//ROCK-scissors win
startActivity(new Intent(MainActivity.this,Win.class));
}
}
});
}
public void paper (View view) {
Button paper = (Button) findViewById(R.id.paper);
paper.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (RPS == 1) {
//PAPER-rock win
startActivity(new Intent(MainActivity.this,Win.class));
} else if (RPS == 2) {
//PAPER-paper tie
startActivity(new Intent(MainActivity.this,Tie.class));
} else if (RPS == 3) {
//PAPER-scissors lose
startActivity(new Intent(MainActivity.this,Lose.class));
}
}
});
}
public void scissors (View v) {
Button scissors = (Button) findViewById(R.id.scissors);
scissors.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (RPS == 1) {
//SCISSORS-rock lose
startActivity(new Intent(MainActivity.this, Lose.class));
} else if (RPS == 2) {
//SCISSORS-paper win
startActivity(new Intent(MainActivity.this, Win.class));
} else if (RPS == 3) {
//SCISSORS-scissors tie
startActivity(new Intent(MainActivity.this, Tie.class));
}
}
});
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}}
When I finished, I noticed none of the "rock" "paper" or "scissor" methods were used. When I ran it on my phone, my buttons didn't do anything, same goes for the emulator.
The message I get (Don't have enough rep to upload)
An explanation and solution of the message would be nice, as I'm still a beginner at this. Any help would be appreciated, thanks!
The message means: you did not call any of those method when running program.
Naive solution: Move all content of 3 methods: rock,... to onCreate()
:
onCreate(...){
...
setContentView(...);
//----Example for rock button
Button Rock = (Button) findViewById(R.id.rock);
Rock.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
/* all capitals = user answer */
if (RPS == 1) {
startActivity(new Intent(MainActivity.this,Tie.class));
//ROCK-rock tie
}
else if (RPS == 2) {
//ROCK-paper lose
startActivity(new Intent(MainActivity.this,Lose.class));
}
else if (RPS == 3) {
//ROCK-scissors win
startActivity(new Intent(MainActivity.this,Win.class));
}
}
});
}
//Do the same for other buttons here
}//end of onCreate
IDE is telling you that those methods were not being called. You didnt post the onCreate contents, you would want to move the button.setOnClick.. over in the onCreate.
On a side note instead of hooking up onclick like that you could do it on your layout file. For example
<Button
android:id="@+id/rock"
android:onclick="rock"
android:text="Rock" />
Here when rock button is clicked it calls "rock" button in your activity.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.