【发布时间】:2015-07-25 05:46:37
【问题描述】:
这让我的导师感到困惑。在用户选择 Rock 按钮、Paper 按钮、Scissors 按钮、Lizard 按钮或 Spock 按钮,然后选择 Submit 按钮后,程序在模拟器中崩溃,并弹出一条消息,说它“意外停止工作”。更奇怪的是,console 或 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.
}
对不起,如果这看起来很有趣,我仍然习惯在这个网站上放代码。
【问题讨论】:
-
你能发布你得到的异常吗?
-
在logcat中查找异常
-
我不明白。我不认为我遇到了例外。
-
OP 已经说过 logcat 中没有显示错误消息...请仔细阅读帖子。但是,话虽如此,为什么不在代码中的每一行之前加入一堆
log.i("LOG", "whereyourprogramis");调用呢?这样,当您的程序终止时,您仍然可以跟踪 logcat 以查看它的运行位置。就目前而言,这是一个巨大的编码转储。请删除不相关的代码。 MVCE 会有很大帮助。 -
@InSeriousNeedOfAspirin :仅仅因为您认为 logcat 中没有任何异常并不意味着没有任何异常。应用程序不会突然抛出“意外停止工作”消息,而不会在某处抛出未处理的异常。
标签: java android eclipse button android-emulator