0

好的,所以我正在为 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)
4

2 回答 2

3

你的第一个问题是nextInt()需要一个大于 0 的数字。提供给nextInt()它的任何东西都会<= 0抛出IllegalArgumentException.

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

于 2012-12-21T01:20:50.890 回答
0

正如您在Random.nextInt(int)的文档中看到的那样,参数的值指定了 0 和参数之间的可用答案范围。您提供的 0 参数是无意义的,因此它会抱怨。

于 2012-12-21T01:25:41.123 回答