石头剪刀布游戏在应用程序中的进度
Rock,Paper,Scissors progress of the game in app
所以我想创建一个应该像这样工作的剪刀石头布应用程序:
在第一个屏幕中,您输入 2 个玩家的名字。
在第二个屏幕中有球员的名字和每个球员的分数接近 it.The 进度是你需要点击一个按钮,点击后,在每个球员的身边,随机图像(石头纸或剪刀)将出现,获胜者将获得一分,如果是平局,则没有人会得分。
注意:根据我在调试时尝试搜索收到的消息时所看到的情况,这可能不是代码中的错误,因此您可能需要先查看它。
不过,我还是希望对代码提出一些意见。
在我开始处理按钮之前,我检查了我从第一个 activity 传递到第二个的名称是否正常,应用程序运行良好。
但是在我为 OnClickListener 编写代码后,当我 运行 它时,应用程序在第一个 activity 之后立即崩溃。这是我第一次使用这样的图像,所以我不确定是否正确使用了它。我已经创建了一些函数,因此在不知道我在做什么的情况下代码将更具可读性,因为我是 android.
的新手
第一个activity:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button startBtn;
startBtn = findViewById(R.id.startBtn);
startBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
EditText p2Name=findViewById(R.id.p2EditText);
EditText p1Name=findViewById(R.id.p1EditText);
String name1=p2Name.getText().toString();
String name2=p1Name.getText().toString();
Intent intent1 = new Intent(MainActivity.this, Game.class);
intent1.putExtra("name1",name1);
intent1.putExtra("name2",name2);
MainActivity.this.startActivity(intent1);
}
});
}
秒activity
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
String p1Name;
String p2Name;
if (getIntent().hasExtra("name1")) {
p1Name = getIntent().getStringExtra("name1");
TextView p1NView = findViewById(R.id.p1);
p1NView.setText(p1Name);
}
if (getIntent().hasExtra("name2")) {
p2Name = getIntent().getStringExtra("name2");
TextView p2NView = findViewById(R.id.p2);
p2NView.setText(p2Name);
}
Button NRound=findViewById(R.id.newRoundBtn);
NRound.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
TextView score1=findViewById(R.id.score1View);
TextView score2=findViewById(R.id.score2View);
int p1Score = Integer.parseInt(score1.getText().toString());
int p2Score = Integer.parseInt(score2.getText().toString());
String[] RPS = new String[]{"rock", "paper", "scissors"};
Random r = new Random();
String p1Hand;
String p2Hand;
p1Hand = RPS[r.nextInt(3)];
p2Hand = RPS[r.nextInt(3)];
showImages(p1Hand,p2Hand);
String result=findWinner(p1Hand,p2Hand);
switch (result){
case "player1":
p1Score++;
score1.setText(String.valueOf(p1Score));
case "player2":
p2Score++;
score2.setText(String.valueOf(p2Score));
if(score1.getText().equals('3') || score2.getText().equals('3')){
Intent end=new Intent(Game.this,EndScreen.class);
Game.this.startActivity(end);
}
}
}
});
update();
}
public static String findWinner(String hand1,String hand2){
if(hand1.equals(hand2)){
return "draw";
}
String both=hand1.concat(hand2);
if(both.equals("rockscissor") || both.equals("paperrock")||both.equals("scissorspaper")){
return "player1";
}else{
return "player2";
}
}
public void showImages(String hand1,String hand2){
ImageView rock1=findViewById(R.id.rock1);
ImageView paper1=findViewById(R.id.paper1);
ImageView scissors1=findViewById(R.id.scissors1);
ImageView rock2=findViewById(R.id.rock2);
ImageView paper2=findViewById(R.id.paper2);
ImageView scissors2=findViewById(R.id.scissors2);
switch (hand1){
case "rock":
rock1.setVisibility(View.VISIBLE);
case "paper":
paper1.setVisibility(View.VISIBLE);
case "scissors":
scissors1.setVisibility(View.VISIBLE);
}
switch (hand2){
case "rock":
rock2.setVisibility(View.VISIBLE);
case "paper":
paper2.setVisibility(View.VISIBLE);
case "scissors":
scissors2.setVisibility(View.VISIBLE);
}
}
public void update(){
ImageView[] images=new ImageView[6];
for (ImageView image:images)
{
if(image.getVisibility()==View.VISIBLE){
image.setVisibility(View.GONE);
}
}
}
日志
Edit:now 在我 post 编辑并查看日志后,我明白了问题出在哪里。我忘记在函数中初始化 imageView 数组,我只是在 null 数组上循环,试图获取它的 visibility.And 现在它不会崩溃。
我不知道这件事,所以还是谢谢你告诉我 post 它。(认为错误应该出现在控制台或其他地方)。
现在我正在处理另一个问题,不过我会尝试自己解决它。
Process: com.example.rockpaperscissors, PID: 11607
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.rockpaperscissors/com.example.rockpaperscissors.Game}: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.widget.ImageView.getVisibility()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3260)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3396)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2009)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7319)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:934)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.widget.ImageView.getVisibility()' on a null object reference
at com.example.rockpaperscissors.Game.update(Game.java:71)
at com.example.rockpaperscissors.Game.onCreate(Game.java:65)
at android.app.Activity.performCreate(Activity.java:7783)
at android.app.Activity.performCreate(Activity.java:7772)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1299)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3235)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3396)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2009)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7319)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:934)
我尝试调试应用程序,在第一行的最后一行 activity,显示的消息是:源代码与字节码不匹配。
首先尝试清理项目并重建它。
为了帮助您解决崩溃问题,我们需要查看日志。也请补充。
关于您的代码:
MainActivity.class
- 在将用户输入传递给下一个 activity 之前尝试验证用户输入。
Game.class(假设这也是一个 activity 所以尝试将其重命名为 GameActivity.class)
当您从 Intent 中检索数据时,最好创建一个常量并使其 public 您也可以在主 activity 中使用(如果你打错了,很难找到问题)
类似于:public static final String NAME_ONE_KEY = "name1";
我认为没有必要使用字符串数组,用整数标识那些操作并对其进行注释,以便您和阅读您代码的其他人能够理解它。
您的游戏布局文件中似乎有很多子元素
将图片添加到drawable文件夹,为每个播放器创建一个图片视图并更新图片源。
最后,保存状态,以免在设备旋转时丢失数据。
所以我想创建一个应该像这样工作的剪刀石头布应用程序: 在第一个屏幕中,您输入 2 个玩家的名字。 在第二个屏幕中有球员的名字和每个球员的分数接近 it.The 进度是你需要点击一个按钮,点击后,在每个球员的身边,随机图像(石头纸或剪刀)将出现,获胜者将获得一分,如果是平局,则没有人会得分。 注意:根据我在调试时尝试搜索收到的消息时所看到的情况,这可能不是代码中的错误,因此您可能需要先查看它。 不过,我还是希望对代码提出一些意见。
在我开始处理按钮之前,我检查了我从第一个 activity 传递到第二个的名称是否正常,应用程序运行良好。 但是在我为 OnClickListener 编写代码后,当我 运行 它时,应用程序在第一个 activity 之后立即崩溃。这是我第一次使用这样的图像,所以我不确定是否正确使用了它。我已经创建了一些函数,因此在不知道我在做什么的情况下代码将更具可读性,因为我是 android.
的新手第一个activity:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button startBtn;
startBtn = findViewById(R.id.startBtn);
startBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
EditText p2Name=findViewById(R.id.p2EditText);
EditText p1Name=findViewById(R.id.p1EditText);
String name1=p2Name.getText().toString();
String name2=p1Name.getText().toString();
Intent intent1 = new Intent(MainActivity.this, Game.class);
intent1.putExtra("name1",name1);
intent1.putExtra("name2",name2);
MainActivity.this.startActivity(intent1);
}
});
}
秒activity
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
String p1Name;
String p2Name;
if (getIntent().hasExtra("name1")) {
p1Name = getIntent().getStringExtra("name1");
TextView p1NView = findViewById(R.id.p1);
p1NView.setText(p1Name);
}
if (getIntent().hasExtra("name2")) {
p2Name = getIntent().getStringExtra("name2");
TextView p2NView = findViewById(R.id.p2);
p2NView.setText(p2Name);
}
Button NRound=findViewById(R.id.newRoundBtn);
NRound.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
TextView score1=findViewById(R.id.score1View);
TextView score2=findViewById(R.id.score2View);
int p1Score = Integer.parseInt(score1.getText().toString());
int p2Score = Integer.parseInt(score2.getText().toString());
String[] RPS = new String[]{"rock", "paper", "scissors"};
Random r = new Random();
String p1Hand;
String p2Hand;
p1Hand = RPS[r.nextInt(3)];
p2Hand = RPS[r.nextInt(3)];
showImages(p1Hand,p2Hand);
String result=findWinner(p1Hand,p2Hand);
switch (result){
case "player1":
p1Score++;
score1.setText(String.valueOf(p1Score));
case "player2":
p2Score++;
score2.setText(String.valueOf(p2Score));
if(score1.getText().equals('3') || score2.getText().equals('3')){
Intent end=new Intent(Game.this,EndScreen.class);
Game.this.startActivity(end);
}
}
}
});
update();
}
public static String findWinner(String hand1,String hand2){
if(hand1.equals(hand2)){
return "draw";
}
String both=hand1.concat(hand2);
if(both.equals("rockscissor") || both.equals("paperrock")||both.equals("scissorspaper")){
return "player1";
}else{
return "player2";
}
}
public void showImages(String hand1,String hand2){
ImageView rock1=findViewById(R.id.rock1);
ImageView paper1=findViewById(R.id.paper1);
ImageView scissors1=findViewById(R.id.scissors1);
ImageView rock2=findViewById(R.id.rock2);
ImageView paper2=findViewById(R.id.paper2);
ImageView scissors2=findViewById(R.id.scissors2);
switch (hand1){
case "rock":
rock1.setVisibility(View.VISIBLE);
case "paper":
paper1.setVisibility(View.VISIBLE);
case "scissors":
scissors1.setVisibility(View.VISIBLE);
}
switch (hand2){
case "rock":
rock2.setVisibility(View.VISIBLE);
case "paper":
paper2.setVisibility(View.VISIBLE);
case "scissors":
scissors2.setVisibility(View.VISIBLE);
}
}
public void update(){
ImageView[] images=new ImageView[6];
for (ImageView image:images)
{
if(image.getVisibility()==View.VISIBLE){
image.setVisibility(View.GONE);
}
}
}
日志 Edit:now 在我 post 编辑并查看日志后,我明白了问题出在哪里。我忘记在函数中初始化 imageView 数组,我只是在 null 数组上循环,试图获取它的 visibility.And 现在它不会崩溃。 我不知道这件事,所以还是谢谢你告诉我 post 它。(认为错误应该出现在控制台或其他地方)。 现在我正在处理另一个问题,不过我会尝试自己解决它。
Process: com.example.rockpaperscissors, PID: 11607
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.rockpaperscissors/com.example.rockpaperscissors.Game}: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.widget.ImageView.getVisibility()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3260)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3396)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2009)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7319)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:934)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.widget.ImageView.getVisibility()' on a null object reference
at com.example.rockpaperscissors.Game.update(Game.java:71)
at com.example.rockpaperscissors.Game.onCreate(Game.java:65)
at android.app.Activity.performCreate(Activity.java:7783)
at android.app.Activity.performCreate(Activity.java:7772)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1299)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3235)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3396)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2009)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7319)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:934)
我尝试调试应用程序,在第一行的最后一行 activity,显示的消息是:源代码与字节码不匹配。
首先尝试清理项目并重建它。 为了帮助您解决崩溃问题,我们需要查看日志。也请补充。
关于您的代码:
MainActivity.class
- 在将用户输入传递给下一个 activity 之前尝试验证用户输入。
Game.class(假设这也是一个 activity 所以尝试将其重命名为 GameActivity.class)
当您从 Intent 中检索数据时,最好创建一个常量并使其 public 您也可以在主 activity 中使用(如果你打错了,很难找到问题) 类似于:public static final String NAME_ONE_KEY = "name1";
我认为没有必要使用字符串数组,用整数标识那些操作并对其进行注释,以便您和阅读您代码的其他人能够理解它。
您的游戏布局文件中似乎有很多子元素 将图片添加到drawable文件夹,为每个播放器创建一个图片视图并更新图片源。
最后,保存状态,以免在设备旋转时丢失数据。