我正在Android工作室写一个Rock Paper Scissors程序,这是我到目前为止所做的:(所有必要的东西都是导入的)
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);
}}
当我完成时,我没有发现任何"摇滚" "纸"或"剪刀"方法被使用。当我在手机上运行时,我的按钮没有做任何事情,模拟器也一样。
The message I get(没有足够的代表上传)
该消息的解释和解决方案将是很好的,因为我仍然是这方面的初学者。任何帮助将不胜感激,谢谢!
答案 0 :(得分:0)
该消息表示:在运行程序时,您没有调用任何这些方法。
天真的解决方案:移动3种方法的所有内容:摇滚,......到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
答案 1 :(得分:0)
IDE告诉您这些方法没有被调用。 你没有发布onCreate内容,你想在onCreate中移动button.setOnClick ..。
在旁边的便条上,而不是挂钩,就像你可以在你的布局文件上做的那样。例如
<Button
android:id="@+id/rock"
android:onclick="rock"
android:text="Rock" />
在点击摇滚按钮时,它会在您的活动中调用“摇滚”按钮。