android - 摇滚,纸,剪刀游戏在应用程序中的进度
问题描述
所以我想创建一个应该像这样工作的 Rock Paper Scissors 应用程序:在第一个屏幕中输入 2 个玩家的姓名。第二个屏幕有球员的名字和附近每个球员的得分。进度是你需要点击一个按钮,点击后,在每个球员的身边,随机图像(石头或剪刀)将出现,获胜者将获得一分,或者如果是平局,则没有人会。 注意:根据我在尝试搜索调试时收到的消息时所看到的内容,这可能不是代码中的错误,因此您可能需要先查看它。 不过,我将不胜感激对代码的一些评论。
在我开始处理按钮之前,我检查了我从第一个活动传递到第二个活动的名称是否正常,并且该应用程序运行良好。但是在我为 OnClickListener 编写代码之后,当我运行它时,该应用程序在第一个活动后立即崩溃。这是我第一次使用这样的图像,所以我不确定是否正确使用它。我创建了一些函数,因此代码将更具可读性,但不知道我在做什么,因为我对 android 还很陌生。
第一项活动:
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);
}
});
}
第二次活动
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);
}
}
}
日志编辑:现在在我发布并查看日志后,我明白了问题所在。我忘记在函数中初始化 imageView 数组,我只是循环空数组,试图获得它的可见性。现在它不会崩溃。我不知道这一点,所以无论如何谢谢你告诉我发布它。(认为错误应该出现在控制台或其他东西中)。现在我正在处理另一个问题,不过我会尝试自己解决。
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)
我尝试调试应用程序,在第一个活动的最后一行,显示的消息是:源代码与字节码不匹配。
解决方案
首先尝试清理项目并重建它。为了让我们帮助您解决崩溃问题,我们需要查看日志。请同时添加。
关于您的代码:
MainActivity.class
- 在将用户输入传递给下一个活动之前尝试验证用户输入。
Game.class(假设这也是一个活动,所以尝试将其重命名为 GameActivity.class)
当您从意图中检索数据时,最佳实践是创建一个常量并将其公开,您也可以在主要活动中使用(如果您打错字,很难找到问题)类似于:public static final字符串 NAME_ONE_KEY = "name1";
我认为不需要使用字符串数组,通过整数识别这些操作并对其进行注释,以便您和其他阅读您的代码的人可以理解它。
您的游戏布局文件中似乎有很多子元素将图像添加到可绘制文件夹中,为每个玩家创建一个图像视图并更新图像源。
最后,保存状态,这样您就不会丢失设备旋转的数据。
推荐阅读
- google-apps-script - 谷歌表的宏
- c++ - C++ 访问者模式重载
- asp.net-mvc-4 - 在iis上发布api后获取该请求的URL长度超过配置的maxUrlLength值只有4行数据
- python-3.x - 汇总 pandas 数据框中多行的数据
- artifactory - 已部署工件标准的工件执行
- hyperledger-fabric - 如何限制对特定组织的“对等链代码升级”的访问
- javascript - 列表未正确加载到 reactjs 程序中
- java - 如何从嵌套的 HashMaps 中检索键而不是其值
- wpf - WPF/ReactiveUI 应用程序中的 ReactiveCommand 阻塞 UI 线程
- javascript - AngularJS路由不会页面