gpt4 book ai didi

android - 应用程序中的Rock,Paper,Scissors进度

转载 作者:行者123 更新时间:2023-12-03 16:41:25 25 4
gpt4 key购买 nike

所以我想创建一个Rock Paper Scissors应用程序,该应用程序应如下所示:
在第一个屏幕中,输入2个播放器的名称。
在第二个屏幕中,有玩家的名称和附近每个玩家的得分。进度是您需要单击一个按钮,单击该按钮后,在每个玩家的侧面,随机图像(石头或剪刀)将会出现,获胜者将获得一个积分,或者如果平局,没人会获得。
注意:根据我尝试搜索调试时收到的消息时所看到的内容,这可能不是代码中的错误,因此您可能首先要看一下。
我希望对代码有一些评论。

在开始使用按钮之前,我检查了从第一个 Activity 传递到第二个 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);
}

}
}

日志
编辑:现在,我发布并查看日志后,我了解了问题所在。我忘了在函数中初始化imageView数组,而只是在遍历null数组,试图获得其可见性。现在它不会崩溃。
我对此一无所知,所以无论如何都要感谢我告诉我发布它(认为错误应该在控制台中显示出来)。
现在我正在处理另一个问题,不过我会尝试自己解决。
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)
  • 从意图中检索数据时,最佳做法是创建一个常量并将其公开,以供您在主 Activity 中使用(如果输入错误,则很难发现问题)
    类似的东西:public static final String NAME_ONE_KEY =“name1”;
  • 我看不到需要使用字符串数组,而是通过整数标识那些操作并对其进行注释,以便您和其他阅读您的代码的人可以理解它。
  • 游戏布局文件中似乎有很多子元素
    将图像添加到drawable文件夹中,为每个播放器创建一个图像 View 并更新图像源。

  • 最后,保存状态,以免旋转设备时丢失数据。

    关于android - 应用程序中的Rock,Paper,Scissors进度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57663195/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com