点击提交按钮后程序崩溃
Posted
技术标签:
【中文标题】点击提交按钮后程序崩溃【英文标题】:Program Crashing After Hitting the Submit Button 【发布时间】: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 中没有任何异常并不意味着没有任何异常。应用程序不会突然抛出“意外停止工作”消息,而不会在某处抛出未处理的异常。
【参考方案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。
【讨论】:
【参考方案2】:查看代码后,我相信这是由于您在 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));
【讨论】:
我做到了,它有效,但是,现在我得到了类似“蜥蜴毒死 Spock.212121212121”的内容。 糟糕!没关系,我写错了。现在它可以正常工作了,但是,胜利、失败和平局仅在第一次增加,然后停止增加。此外,有时,我点击一个按钮,然后点击提交,没有任何反应。 在文本中显示之前移动赢/输/平的增量。 comWins++; compWinsTextView.setText(getResources().getString (R.string.comp_wins, compWins)); 好的,它正在工作,但是每当我获得平局时,它就会增加损失而不是平局。 :( 在 loadNextRound 中重新排列最后两个 if 块,您的平局永远不会被击中,因为您的块设置方式是赢/输。以上是关于点击提交按钮后程序崩溃的主要内容,如果未能解决你的问题,请参考以下文章