点击提交按钮后程序崩溃

时间:2015-05-13 19:06:29

标签: java android eclipse button android-emulator

这是我的导师和我难倒。用户选择“摇滚”按钮,“纸张”按钮,“剪刀”按钮,“蜥蜴”按钮或“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.
}

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

2 个答案:

答案 0 :(得分:1)

您没有在结果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查找与该字符串关联的字符串ID为56(20 + 36)。

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

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

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

答案 1 :(得分:0)

在查看代码后,我认为这是由于您在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));