点击提交按钮后程序崩溃

时间:2021-11-20 20:46:45

This has my instructor and I stumped. After the user selects either the Rock button, Paper button, Scissors button, Lizard button, or Spock button and then selects the Submit button, the program crashes in the emulator and a message pops up saying that it "Unexpectedly Stopped Working." What's even more strange is that there are no error messages in the console or logcat. Also, when the user's choice and the computer's choice are the same, Ties does NOT increment, but the Losses does. I'm not sure what exactly is going on. I thought that maybe a fresh pair of eyes would help, since I am new to Android and I stumped my instructor. Thank you. Here is the code for GameFragment.java. As you can see, I split it into multiple functions to make it easier to call things elsewhere, instead of re-writing code.

这有我的导师和我难倒。用户选择“摇滚”按钮,“纸张”按钮,“剪刀”按钮,“蜥蜴”按钮或“Spock”​​按钮,然后选择“提交”按钮后,程序将在模拟器中崩溃并弹出一条消息,说明“意外停止工作”。更奇怪的是控制台或logcat中没有错误消息。此外,当用户的选择和计算机的选择相同时,领带不会增加,但损失会增加。我不确定到底发生了什么。我想也许一副新鲜的眼睛会有所帮助,因为我是Android的新手而且我难倒我的导师。谢谢。这是GameFragment.java的代码。正如您所看到的,我将其拆分为多个函数,以便更容易在其他地方调用,而不是重写代码。

import java.util.Random;

import android.app.AlertDialog;
import android.app.Fragment;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;


public class GameFragment extends Fragment implements OnClickListener
{
 //private static final String TAG = "RockPaperScissorsLizardSpock Activity";

 private int currentRound;
 private int yourWins = 0;
 private int compWins = 0;
 private int ties = 0;
 private int computerPick;
 private int playerPick;
 private int rock = 1;
 private int paper = 2;
 private int scissors = 3;
 private int lizard = 4;
 private int spock = 5;
 private Animation shake;
 private TextView roundTextView;
 private TextView playerWinsTextView;
 private TextView compWinsTextView;
 private TextView resultsTextView;
 private TextView tiesTextView;
 private Handler handler;
 private boolean didPlayerWin = false;
 private boolean isATie = false;

 private ImageButton rockImageButton;
 private ImageButton vaporizedRockImageButton;
 private ImageButton paperImageButton;
 private ImageButton scissorsImageButton;
 private ImageButton lizardImageButton;
 private ImageButton decapitatedLizardImageButton;
 private ImageButton spockImageButton;

 private Button rulesButton;
 private Button submitButton;
 private Button restartButton;


 @Override
 public View onCreateView(LayoutInflater inflater, ViewGroup container, 
                            Bundle savedInstanceState)
 {
     super.onCreateView(inflater, container, savedInstanceState);
     View view = 
             inflater.inflate(R.layout.fragment_game, container, false);

     shake = AnimationUtils.loadAnimation(getActivity(), R.anim.shake);
     shake.setRepeatCount(3);
     handler = new Handler();

     // References to the TextViews
     roundTextView = (TextView)
             view.findViewById(R.id.roundTextView);  
     playerWinsTextView = (TextView)
             view.findViewById(R.id.playerWinsTextView);
     compWinsTextView = (TextView)
             view.findViewById(R.id.compWinsTextView);
     resultsTextView = (TextView)
             view.findViewById(R.id.resultsTextView);
     tiesTextView = (TextView) 
             view.findViewById(R.id.tiesTextView);

     // References to the ImageButtons
     rockImageButton = (ImageButton)
             view.findViewById(R.id.rockImageButton);
     vaporizedRockImageButton = (ImageButton)
             view.findViewById(R.id.vaporizedRockImageButton);
     paperImageButton = (ImageButton)
             view.findViewById(R.id.paperImageButton);
     scissorsImageButton = (ImageButton)
             view.findViewById(R.id.scissorsImageButton);
     lizardImageButton = (ImageButton)
             view.findViewById(R.id.lizardImageButton);
     decapitatedLizardImageButton = (ImageButton)
             view.findViewById(R.id.decapitatedLizardImageButton);
     spockImageButton = (ImageButton)
             view.findViewById(R.id.spockImageButton);

     // References to the Buttons
     rulesButton = (Button)
             view.findViewById(R.id.rulesButton);
     submitButton = (Button)
             view.findViewById(R.id.submitButton);
     restartButton = (Button)
             view.findViewById(R.id.restartButton);

     rulesButton.setOnClickListener(this);
     submitButton.setOnClickListener(this);
     restartButton.setOnClickListener(this);

     rockImageButton.setOnClickListener(this);
     vaporizedRockImageButton.setOnClickListener(this);
     paperImageButton.setOnClickListener(this);
     scissorsImageButton.setOnClickListener(this);
     lizardImageButton.setOnClickListener(this);
     decapitatedLizardImageButton.setOnClickListener(this);
     spockImageButton.setOnClickListener(this);

     // Set the text for the TextViews
     currentRound = 1;

     roundTextView.setText(getResources().getString
             (R.string.round, currentRound));
     playerWinsTextView.setText(getResources().getString
             (R.string.player_wins, yourWins));
     compWinsTextView.setText(getResources().getString
             (R.string.comp_wins, compWins));
     tiesTextView.setText(getResources().getString
             (R.string.num_ties, ties));

     generateRandomNum();

     return view; 
 }  // End of onCreateView

 public void resetGame()
 {
     clearRounds();
     clearPlayerWinsAndCompWins();
     generateRandomNum();

 }

 public void clearPlayerWinsAndCompWins()
 {
     yourWins = 0;
     compWins = 0;
     ties     = 0;

     playerWinsTextView.setText(getResources().getString
             (R.string.player_wins, yourWins));
     compWinsTextView.setText(getResources().getString
             (R.string.comp_wins, compWins));
     tiesTextView.setText(getResources().getString
             (R.string.num_ties, ties));
 }

 public void clearRounds()
 {
     currentRound = 1;

     roundTextView.setText(getResources().getString
             (R.string.round, currentRound));
 }

 public void generateRandomNum()
 {
     final Random rand = new Random();
     computerPick = rand.nextInt(5) + 1;
     computerPick++;
 }

 public void gameLogic()
 {
     isATie = false;
     didPlayerWin = false;

     if (playerPick == rock && computerPick == paper)
     {
         resultsTextView.setText(R.string.paper_beats_rock + R.string.you_lose);
         resultsTextView.setTextColor(
                 getResources().getColor(R.color.you_lose_color));
         didPlayerWin = false;
         theHandler();
     }

     else if (playerPick == paper && computerPick == rock)
     {
         resultsTextView.setText(R.string.paper_beats_rock + R.string.you_won);
         resultsTextView.setTextColor(
                 getResources().getColor(R.color.you_win_color));
         didPlayerWin = true;
         theHandler();
     }

     else if (playerPick == scissors && computerPick == paper)
     {
         resultsTextView.setText
                (R.string.scissors_cuts_paper + R.string.you_won);
         resultsTextView.setTextColor(
                 getResources().getColor(R.color.you_win_color));
         didPlayerWin = true;
         theHandler();
     }

     else if (playerPick == paper && computerPick == scissors)
     {
         resultsTextView.setText
                (R.string.scissors_cuts_paper + R.string.you_lose);
         resultsTextView.setTextColor(
                 getResources().getColor(R.color.you_lose_color));
         didPlayerWin = false;
         theHandler();
     }

     else if (playerPick == rock && computerPick == scissors)
     {
         resultsTextView.setText
            (R.string.rock_crushes_scissors + R.string.you_won);
         resultsTextView.setTextColor(
             getResources().getColor(R.color.you_win_color));
         didPlayerWin = true;
         theHandler();
     }

     else if (playerPick == scissors && computerPick == rock)
     {
         resultsTextView.setText
            (R.string.rock_crushes_scissors + R.string.you_lose);
         resultsTextView.setTextColor(
             getResources().getColor(R.color.you_lose_color));
         didPlayerWin = false;
         theHandler();
     }

     else if (playerPick == lizard && computerPick == spock)
     {
         resultsTextView.setText
            (R.string.lizard_poisons_spock + R.string.you_won);
         resultsTextView.setTextColor(
             getResources().getColor(R.color.you_win_color));
         didPlayerWin = true;
         theHandler();
     }

     else if (playerPick == spock && computerPick == lizard)
     {
         resultsTextView.setText
            (R.string.lizard_poisons_spock + R.string.you_lose);
         resultsTextView.setTextColor(
             getResources().getColor(R.color.you_lose_color));
         didPlayerWin = false;
         theHandler();
     }

     else if (playerPick == lizard && computerPick == paper)
     {
         resultsTextView.setText
            (R.string.lizard_eats_paper + R.string.you_won);
         resultsTextView.setTextColor(
             getResources().getColor(R.color.you_win_color));
         didPlayerWin = true;
         theHandler();
     }

     else if (playerPick == paper && computerPick == lizard)
     {
         resultsTextView.setText
            (R.string.lizard_eats_paper + R.string.you_lose);
         resultsTextView.setTextColor(
             getResources().getColor(R.color.you_lose_color));
         didPlayerWin = false;
         theHandler();
     }

     else if (playerPick == rock && computerPick == spock)
     {
         resultsTextView.setText
            (R.string.spock_vaporizes_rock + R.string.you_lose);
         resultsTextView.setTextColor(
             getResources().getColor(R.color.you_lose_color));
         vaporizedRockImageButton.setVisibility(View.VISIBLE);
         didPlayerWin = false;
         theHandler();
     }

     else if (playerPick == spock && computerPick == rock)
     {
         resultsTextView.setText
            (R.string.spock_vaporizes_rock + R.string.you_won);
         resultsTextView.setTextColor(
             getResources().getColor(R.color.you_win_color));
         didPlayerWin = true;
         vaporizedRockImageButton.setVisibility(View.VISIBLE);
         theHandler();
     }

     else if (playerPick == paper && computerPick == spock)
     {
         resultsTextView.setText
            (R.string.paper_disproves_spock + R.string.you_won);
         resultsTextView.setTextColor(
             getResources().getColor(R.color.you_win_color));
         didPlayerWin = true;
         theHandler();
     }
     else if (playerPick == spock && computerPick == paper)
     {
         resultsTextView.setText
            (R.string.paper_disproves_spock + R.string.you_lose);
         resultsTextView.setTextColor(
             getResources().getColor(R.color.you_lose_color));
         didPlayerWin = false;
         theHandler();
     }

     else if (playerPick == lizard && computerPick == rock)
     {
         resultsTextView.setText
            (R.string.rock_crushes_lizard + R.string.you_lose);
         resultsTextView.setTextColor(
             getResources().getColor(R.color.you_lose_color));
         didPlayerWin = false;
         theHandler();
     }
     else if (playerPick == rock && computerPick == lizard)
     {
         resultsTextView.setText
            (R.string.rock_crushes_lizard + R.string.you_won);
         resultsTextView.setTextColor(
             getResources().getColor(R.color.you_win_color));
         didPlayerWin = true;
         theHandler();
     }
     else if (playerPick == lizard && computerPick == scissors)
     {
         resultsTextView.setText
            (R.string.scissors_decapitates_lizard + R.string.you_lose);
         resultsTextView.setTextColor(
             getResources().getColor(R.color.you_lose_color));
         decapitatedLizardImageButton.setVisibility(View.VISIBLE);
         didPlayerWin = false;
         theHandler();
     }
     else if (playerPick == scissors && computerPick == lizard)
     {
        resultsTextView.setText
            (R.string.scissors_decapitates_lizard + R.string.you_won);
        resultsTextView.setTextColor(
             getResources().getColor(R.color.you_win_color)); 
        decapitatedLizardImageButton.setVisibility(View.VISIBLE);
        didPlayerWin = true;
        theHandler();
     }

     else if (playerPick == computerPick)
     {
         resultsTextView.setText
            (R.string.its_a_tie);
        resultsTextView.setTextColor(
             getResources().getColor(R.color.you_lose_color));
         isATie = true;
         didPlayerWin = false;
         theHandler();
     }
 }


public void onClick(View v) 
{
    //v = (ImageButton) v;
    if (v == rulesButton)
    {
        showRules();
    }
    if (v == restartButton)
    {
        resetGame();
    }
    if (v == submitButton)
    {
        gameLogic();
    }
    if (v == rockImageButton)
    {
        playerPick = rock;
    }
    if (v == paperImageButton)
    {
        playerPick = paper;
    }
    if (v == scissorsImageButton)
    {
        playerPick = scissors;
    }
    if (v == lizardImageButton)
    {
        playerPick = lizard;
    }
    if (v == spockImageButton)
    {
        playerPick = spock;
    }  

}



public void showRules()
{
    AlertDialog.Builder a1 = new AlertDialog.Builder(getActivity());

    // Setting Dialog Title
    a1.setTitle("RULES");

    // Setting Dialog Message
    a1.setMessage("Here are the rules for Rock Paper Scissors Lizard Spock:\n\n" 
            + "\t-Paper beats Rock" + "\n\t-Rock beats Scissors" + 
            "\n\t-Scissors beats Paper" + "\n\t-Rock crushes Lizard" + 
            "\n\t-Lizard poisons Spock" + "\n\t-Spock smashes Scissors" + 
            "\n\t-Scissors decapitate Lizard" + "\n\t-Lizard eats Paper" 
            + "\n\t-Paper disproves Spock" + "\n\t-Spock vaporizes Rock" 
            + "\n\nIf there is a tie, the round will continue until a " + 
            "winner is found.");

    // Setting OK Button
    a1.setPositiveButton("OK", new DialogInterface.OnClickListener() 
    {
        //@Override
        public void onClick(DialogInterface dialog, int which) 
        {
    // Write your code here to execute after dialog closed

            dialog.dismiss();                   
        }
    });

    // Showing Alert Message
    @SuppressWarnings("unused")
    AlertDialog alertDialog = a1.create();
    a1.show();

}  


// Handler method for loading the next round
public void theHandler()
{
    handler.postDelayed(
        new Runnable()
        {
            @Override
            public void run()
            {
                loadNextRound();
            }

        }, 2000);
}

// loadNextRound method loads the next round and updates the textviews
// and image buttons (if needed).
public void loadNextRound()
{
    if (didPlayerWin == true)
    {
        resultsTextView.setText("");

        roundTextView.setText(getResources().getString
                (R.string.round, (currentRound + 1)));

        playerWinsTextView.setText(getResources().getString
                 (R.string.player_wins, (yourWins + 1)));
        didPlayerWin = false;

        generateRandomNum();
    }
    else if (didPlayerWin == false)
    {
        resultsTextView.setText("");

        roundTextView.setText(getResources().getString
                (R.string.round, (currentRound + 1)));

        compWinsTextView.setText(getResources().getString
                 (R.string.comp_wins, (compWins + 1)));
        didPlayerWin = false;

        generateRandomNum();
    }
    else if (isATie == true)
    {
        resultsTextView.setText("");

        tiesTextView.setText(getResources().getString
                (R.string.num_ties, (ties + 1)));
        isATie = false;

        generateRandomNum();
    }
    //generateRandomNum();
    vaporizedRockImageButton.setVisibility(View.INVISIBLE);
    decapitatedLizardImageButton.setVisibility(View.INVISIBLE);

  } // End of loadNextRound method.
}

Sorry if this looks funny, I'm still getting used to putting code on this site.

对不起,如果这看起来很有趣,我仍然习惯于在这个网站上放置代码。

2 个解决方案

#1


After looking at the code I believe that it is due to how you are setting the text in gameLogic(). change:

查看代码后,我认为这是由于您在gameLogic()中设置文本的方式。更改:

resultsTextView.setText(R.string.paper_beats_rock + R.string.you_lose);

resultsTextView.setText(R.string.paper_beats_rock + R.string.you_lose);

to:

resultsTextView.setText(getString(R.string.paper_beats_rock) + getString(R.string.you_lose));

#2


You are not setting the text on your results TextView properly.

您没有正确地在结果TextView上设置文本。

The problem is at lines such as this:

问题出现在以下几行:

resultsTextView.setText(R.string.paper_beats_rock + R.string.you_lose);

R.string.paper_beats_rock is an integer. It refers to a string defined in your strings.xml, but is not the string itself.

R.string.paper_beats_rock是一个整数。它引用strings.xml中定义的字符串,但不是字符串本身。

If we assume that the ID of your paper_beats_rock string is 20 and the ID for you_lose is 36, then what you are really saying is that you want Android to look up the string associated with the ID of 56 (20 + 36).

如果我们假设你的paper_beats_rock字符串的ID是20而you_lose的ID是36,那么你真正要说的是你希望Android查找与ID为56(20 + 36)相关联的字符串。

Due to the way IDs are generated, there is a very good chance that no string with that ID exists, and your application will crash with a ResourceNotFoundException.

由于生成ID的方式,很可能不存在具有该ID的字符串,并且您的应用程序将因ResourceNotFoundException而崩溃。

If you want to concatenate the strings, you should call getString() on those IDs first.

如果要连接字符串,则应首先对这些ID调用getString()。

This exception will appear in your LogCat as well, so it sounds like you also aren't quite looking at your logcat correctly.

此异常也将出现在您的LogCat中,因此听起来您还没有正确地查看您的logcat。

#1


After looking at the code I believe that it is due to how you are setting the text in gameLogic(). change:

查看代码后,我认为这是由于您在gameLogic()中设置文本的方式。更改:

resultsTextView.setText(R.string.paper_beats_rock + R.string.you_lose);

resultsTextView.setText(R.string.paper_beats_rock + R.string.you_lose);

to:

resultsTextView.setText(getString(R.string.paper_beats_rock) + getString(R.string.you_lose));

#2


You are not setting the text on your results TextView properly.

您没有正确地在结果TextView上设置文本。

The problem is at lines such as this:

问题出现在以下几行:

resultsTextView.setText(R.string.paper_beats_rock + R.string.you_lose);

R.string.paper_beats_rock is an integer. It refers to a string defined in your strings.xml, but is not the string itself.

R.string.paper_beats_rock是一个整数。它引用strings.xml中定义的字符串,但不是字符串本身。

If we assume that the ID of your paper_beats_rock string is 20 and the ID for you_lose is 36, then what you are really saying is that you want Android to look up the string associated with the ID of 56 (20 + 36).

如果我们假设你的paper_beats_rock字符串的ID是20而you_lose的ID是36,那么你真正要说的是你希望Android查找与ID为56(20 + 36)相关联的字符串。

Due to the way IDs are generated, there is a very good chance that no string with that ID exists, and your application will crash with a ResourceNotFoundException.

由于生成ID的方式,很可能不存在具有该ID的字符串,并且您的应用程序将因ResourceNotFoundException而崩溃。

If you want to concatenate the strings, you should call getString() on those IDs first.

如果要连接字符串,则应首先对这些ID调用getString()。

This exception will appear in your LogCat as well, so it sounds like you also aren't quite looking at your logcat correctly.

此异常也将出现在您的LogCat中,因此听起来您还没有正确地查看您的logcat。