点击提交按钮后程序崩溃

Program Crashing After Hitting the Submit Button

这让我的导师和我都难住了。当用户选择摇滚按钮、布按钮、剪刀按钮、蜥蜴按钮或 Spock 按钮然后选择提交按钮后,程序在模拟器中崩溃并弹出一条消息说它 "Unexpectedly Stopped Working." 更重要的是奇怪的是,控制台或logcat中没有任何错误消息。此外,当用户的选择和计算机的选择相同时,Ties 不会增加,但 Losses 会增加。我不确定到底发生了什么。我想也许一双新眼睛会有所帮助,因为我是 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.
}

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

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

问题出在这样的行:

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

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

如果我们假设您的 paper_beats_rock 字符串的 ID 是 20 而 you_lose 的 ID 是 36,那么您真正的意思是您希望 Android 看起来up 与 ID 相关联的字符串 56 (20 + 36).

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

如果您想连接字符串,您应该先对这些 ID 调用 getString()

这个异常 也会 出现在您的 LogCat 中,因此听起来您也没有完全正确地查看您的 logcat。

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

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

至:

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