gpt4 book ai didi

java - 不幸的是<程序名称>已停止。 Android 石头剪刀布游戏

转载 作者:行者123 更新时间:2023-12-02 00:05:29 25 4
gpt4 key购买 nike

好吧,我正在为 Android 制作一个简单的石头剪刀布游戏。到目前为止,我有以下内容:

package com.example.digitoolz;

import java.util.Random;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.Toast;

public class GameActivity extends Activity {
private int userChoice;
private int compChoice;

private int userScore = 0;
private int compScore = 0;

private Toast winnerToast;

public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.game_button);


ImageButton rockbutton = (ImageButton)findViewById(R.id.rock_button);
rockbutton.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
userChoice = 0;
compChoiceSelector();
winSelect();
}
});

ImageButton paperbutton = (ImageButton)findViewById(R.id.paper_button);
paperbutton.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
userChoice = 1;
compChoiceSelector();
winSelect();
}
});


ImageButton scissorsbutton = (ImageButton)findViewById(R.id.scissors_button);
scissorsbutton.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
userChoice = 2;
compChoiceSelector();
winSelect();
}
});
}

public void compChoiceSelector(){
Random ran = new Random();
compChoice = ran.nextInt(0) + 3;
}

public void winSelect(){
if(userChoice == compChoice){
winnerToast.setText("It's a Draw!");
}else if (userChoice == 0 && compChoice == 1){
winnerToast.setText("Paper beats Rock! You Lose!");
compScore++;
}else if (userChoice == 0 && compChoice == 2){
winnerToast.setText("Rock beats Scissorcs! You Win!");
userScore++;
}else if (userChoice == 1 && compChoice == 0){
winnerToast.setText("Paper beats Rock! You Win!");
userScore++;
}else if (userChoice == 1 && compChoice == 2){
winnerToast.setText("Scissors beats paper! You Lose!");
compScore++;
}else if (userChoice == 2 && compChoice == 0){
winnerToast.setText("Rock beats scissors! You Lose!");
compScore++;
}else if (userChoice == 2 && compChoice == 1){
winnerToast.setText("Scissors beats paper... You Win.");
userScore++;
}else{
winnerToast.setText("");
}
}
}

基本上,当按下 3 个按钮(石头、布或剪刀)中的任何一个时。我收到一个弹出窗口,显示“不幸的是,digitoolz 已停止工作。

我不知道为什么会发生这种情况!?请有人帮助我!

提前感谢大家! :)

12-21 01:02:19.711: D/AndroidRuntime(573): Shutting down VM
12-21 01:02:19.721: W/dalvikvm(573): threadid=1: thread exiting with uncaught exception (group=0x409961f8)
12-21 01:02:19.782: E/AndroidRuntime(573): FATAL EXCEPTION: main
12-21 01:02:19.782: E/AndroidRuntime(573): java.lang.IllegalArgumentException
12-21 01:02:19.782: E/AndroidRuntime(573): at java.util.Random.nextInt(Random.java:187)
12-21 01:02:19.782: E/AndroidRuntime(573): at com.example.digitoolz.GameActivity.compChoiceSelector(GameActivity.java:59)
12-21 01:02:19.782: E/AndroidRuntime(573): at com.example.digitoolz.GameActivity$2.onClick(GameActivity.java:40)
12-21 01:02:19.782: E/AndroidRuntime(573): at android.view.View.performClick(View.java:3480)
12-21 01:02:19.782: E/AndroidRuntime(573): at android.view.View$PerformClick.run(View.java:13983)
12-21 01:02:19.782: E/AndroidRuntime(573): at android.os.Handler.handleCallback(Handler.java:605)
12-21 01:02:19.782: E/AndroidRuntime(573): at android.os.Handler.dispatchMessage(Handler.java:92)
12-21 01:02:19.782: E/AndroidRuntime(573): at android.os.Looper.loop(Looper.java:137)
12-21 01:02:19.782: E/AndroidRuntime(573): at android.app.ActivityThread.main(ActivityThread.java:4340)
12-21 01:02:19.782: E/AndroidRuntime(573): at java.lang.reflect.Method.invokeNative(Native Method)
12-21 01:02:19.782: E/AndroidRuntime(573): at java.lang.reflect.Method.invoke(Method.java:511)
12-21 01:02:19.782: E/AndroidRuntime(573): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
12-21 01:02:19.782: E/AndroidRuntime(573): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
12-21 01:02:19.782: E/AndroidRuntime(573): at dalvik.system.NativeStart.main(Native Method)
12-21 01:07:20.110: I/Process(573): Sending signal. PID: 573 SIG: 9

新的 Logcat:

12-21 02:33:17.920: D/AndroidRuntime(11568): Shutting down VM
12-21 02:33:17.920: W/dalvikvm(11568): threadid=1: thread exiting with uncaught exception (group=0x40abca08)
12-21 02:33:17.930: E/AndroidRuntime(11568): FATAL EXCEPTION: main
12-21 02:33:17.930: E/AndroidRuntime(11568): java.lang.RuntimeException: This Toast was not created with Toast.makeText()
12-21 02:33:17.930: E/AndroidRuntime(11568): at android.widget.Toast.setText(Toast.java:277)
12-21 02:33:17.930: E/AndroidRuntime(11568): at com.example.digitoolz.GameActivity.winSelect(GameActivity.java:65)
12-21 02:33:17.930: E/AndroidRuntime(11568): at com.example.digitoolz.GameActivity$2.onClick(GameActivity.java:42)
12-21 02:33:17.930: E/AndroidRuntime(11568): at android.view.View.performClick(View.java:3538)
12-21 02:33:17.930: E/AndroidRuntime(11568): at android.view.View$PerformClick.run(View.java:14319)
12-21 02:33:17.930: E/AndroidRuntime(11568): at android.os.Handler.handleCallback(Handler.java:608)
12-21 02:33:17.930: E/AndroidRuntime(11568): at android.os.Handler.dispatchMessage(Handler.java:92)
12-21 02:33:17.930: E/AndroidRuntime(11568): at android.os.Looper.loop(Looper.java:156)
12-21 02:33:17.930: E/AndroidRuntime(11568): at android.app.ActivityThread.main(ActivityThread.java:5045)
12-21 02:33:17.930: E/AndroidRuntime(11568): at java.lang.reflect.Method.invokeNative(Native Method)
12-21 02:33:17.930: E/AndroidRuntime(11568): at java.lang.reflect.Method.invoke(Method.java:511)
12-21 02:33:17.930: E/AndroidRuntime(11568): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
12-21 02:33:17.930: E/AndroidRuntime(11568): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
12-21 02:33:17.930: E/AndroidRuntime(11568): at dalvik.system.NativeStart.main(Native Method)

最佳答案

你的第一个问题是nextInt()需要一个大于 0 的数字。提供给 nextInt() 的任何内容即<= 0抛出IllegalArgumentException .

您的其他问题可能是:您似乎没有实例化您的 winnerToast 。它是全局的,但您的代码实际上从未这样做 winnerToast = new Toast(this);您可能应该在 onCreate() 中添加这行代码避免 NullPointerException 的方法.

关于java - 不幸的是<程序名称>已停止。 Android 石头剪刀布游戏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13982982/

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