所以我不得不在应用程序上玩游戏Craps,我不得不修改它以允许带有规则的主屏幕,买入系统和投注系统。直到这个修改,应用程序完全没有问题,但添加修改后,应用程序崩溃我启动它的瞬间。在这一点上,我找不到任何修复它的东西,并且其他线程上列出的答案似乎不适用,所以我转向SO寻求帮助。以下是我的所有代码: `
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".CrapsGame">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
<resources>
<string name="app_name">Craps!</string>
<string name="Banner_Text">Try your luck!</string>
<string name="roll_Total">2</string>
<string name="players_Wincount">Your wins: 0</string>
<string name="house_Wincount">House wins: 0</string>
<string name="button_Name">Roll!</string>
<string name="player_Wins">You won!</string>
<string name="Point">Point: </string>
<string name="house_Wins">Better luck next time!</string>
<string name="rulesminBuy">Minimum buy-in is $5.00, no change!</string>
<string name="rulesmaxBuy">Maximum buy-in is $200.00, no change!</string>
<string name="rulesminBet">Minimum bet is $1.00</string>
<string name="rulesmaxBet">Maximum bet is $50.00</string>
<string name="rulesgameRules">Roll a 7 or 11 on your first roll and you win twice your bet!</string>
<string name="rulesgameRulesTwo">Roll a 2, 3, or 12 and you lose twice your bet!</string>
<string name="rulesgameRulesThree">Roll anything else and that becomes your point!</string>
<string name="rulesgameRulesFour">Roll your point again to win, but if you roll a 7 you lose!</string>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@+id/die1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
app:srcCompat="@drawable/die1" />
<TextView
android:id="@+id/rollTotal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textAlignment="center"
android:layout_weight="1"
android:text="@string/roll_Total" />
<ImageView
android:id="@+id/die2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
app:srcCompat="@drawable/die1" />
</LinearLayout>
<TextView
android:id="@+id/pointbox"
android:textAlignment="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/Point" />
<TextView
android:id="@+id/playerWins"
android:layout_width="match_parent"
android:textAlignment="center"
android:layout_height="wrap_content"
android:text="@string/players_Wincount" />
<TextView
android:id="@+id/houseWins"
android:textAlignment="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/house_Wincount" />
<Button
android:id="@+id/OrderButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/button_Name"
android:textAlignment="center" />
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/bannerTwo"
android:textAlignment="center"
android:layout_gravity="center"
android:textColor="@android:color/black"
android:background="@android:color/background_light"
android:layout_width="match_parent"
android:textSize="50px"
android:layout_height="wrap_content"
android:text="@string/Banner_Text"
android:layout_weight="0.15" />
<EditText
android:id="@+id/inputAmmount"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="$5.00 minimum"
android:textAlignment="center"
android:inputType="number" />
<Button
android:id="@+id/buyInButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.15"
android:text="Buy In!" />
<TextView
android:id="@+id/textView5"
android:textAlignment="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/rulesgameRules" />
<TextView
android:id="@+id/textView16"
android:textAlignment="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/rulesgameRulesTwo" />
<TextView
android:id="@+id/textView15"
android:textAlignment="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/rulesgameRulesThree" />
<TextView
android:id="@+id/textView14"
android:textAlignment="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/rulesgameRulesFour" />
<TextView
android:id="@+id/textView13"
android:textAlignment="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/rulesminBuy" />
<TextView
android:id="@+id/textView12"
android:textAlignment="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/rulesmaxBuy" />
<TextView
android:id="@+id/textView11"
android:textAlignment="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/rulesminBet" />
<TextView
android:id="@+id/textView10"
android:textAlignment="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/rulesmaxBet" />
</LinearLayout>
package com.example.kasresredux.akeelin3_groupassignment01;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.Random;
public class CrapsGame extends AppCompatActivity {
//Developer note: as it was not specifically asked for, nor do I expect people to understand how to do it in an introduction
//class, I didn't include any way for the computer to save the information on screen rotation, so the app does kill and restart.
//Create the random function as well as the required variables. They're outside of a method so they can be used anywhere.
Random randomRoll = new Random();
int dieRollOne, dieRollTwo = 1;
int tempRoll;
int rollTotal = 2;
boolean isFirstDie, firstRoll = true;
int playerWins = 0;
int houseWins = 0;
int pointValue = 0;
int buyInAmmount = 0;
int betAmount = 1;
int currentTotalValue;
//onCreate method used to handle layout control and to handle the rolls in a separate method.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home_screen);
final Button buyInButton = (Button) findViewById(R.id.buyInButton);
//Check that the buy-in is legal and, if it is, proceed to the game.
buyInButton.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
final EditText buyInCounter = (EditText) findViewById(R.id.inputAmmount);
try {
buyInAmmount = Integer.parseInt(buyInCounter.toString());
}
catch (NumberFormatException e){
}
if (buyInAmmount >= 5.00)
{
currentTotalValue = buyInAmmount;
Toast.makeText(getApplicationContext(), "Welcome, and good luck!", Toast.LENGTH_LONG).show();
setContentView(R.layout.activity_craps_game);
}
else if (buyInCounter == null)
{
Toast.makeText(getApplicationContext(), "Please enter your buy-in amount!", Toast.LENGTH_LONG).show();
}
else if (buyInAmmount <= 4.99)
{
Toast.makeText(getApplicationContext(), "Please enter at least a five dollar minimum buy in!", Toast.LENGTH_LONG).show();
}
else if (buyInAmmount > 500)
{
Toast.makeText(getApplicationContext(), "The maximum buy-in is $500!", Toast.LENGTH_LONG).show();
}
}
});
//This may be unneeded, but I wanted to be certain.
if (getResources().getDisplayMetrics().widthPixels < getResources().getDisplayMetrics().heightPixels) {
final Button buttonRoll = (Button) findViewById(R.id.OrderButton);
buttonRoll.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
rollHandler();
}
});
}
else if (getResources().getDisplayMetrics().widthPixels > getResources().getDisplayMetrics().heightPixels) {
final Button buttonRoll = (Button) findViewById(R.id.OrderButton);
buttonRoll.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
rollHandler();
}
});
}
final Button increaseBetButton = (Button) findViewById(R.id.increaseBet);
final Button descreaseBetButton = (Button) findViewById(R.id.lowerBet);
increaseBetButton.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
increaseBet();
}
});
descreaseBetButton.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
decreaseBet();
}
});
}
//Main code for deciding the rolls.
public void rollResults() {
//Reset rollTotal so it can't go infinitely large.
rollTotal = 0;
//Decide that this is for the first of two dice images.
isFirstDie = true;
tempRoll = randomRoll.nextInt(6);
dieRollOne = decideImage(tempRoll, isFirstDie);
//Add the dieRoll result RATHER than tempRoll as tempRoll will be one lower than we need, and we aren't rolling 1d6-1.
rollTotal += dieRollOne;
isFirstDie = false;
tempRoll = randomRoll.nextInt(6);
dieRollTwo = decideImage(tempRoll, isFirstDie);
rollTotal += dieRollTwo;
}
//Method used to change the image of the die.
public int decideImage(int roll, boolean dieRollOne) {
//Find the die to change, and set that image to what it should be. Sadly, no fancy roll animations will be included.
ImageView i = (ImageView) findViewById(R.id.die1);
ImageView u = (ImageView) findViewById(R.id.die2);
if (dieRollOne == true) {
switch (roll) {
case 0:
i.setImageResource(R.drawable.die1);
return 1;
case 1:
i.setImageResource(R.drawable.die2);
return 2;
case 2:
i.setImageResource(R.drawable.die3);
return 3;
case 3:
i.setImageResource(R.drawable.die4);
return 4;
case 4:
i.setImageResource(R.drawable.die5);
return 5;
case 5:
i.setImageResource(R.drawable.die6);
return 6;
default:
return 0;
}
} else if (dieRollOne == false) {
switch (roll) {
case 0:
u.setImageResource(R.drawable.die1);
return 1;
case 1:
u.setImageResource(R.drawable.die2);
return 2;
case 2:
u.setImageResource(R.drawable.die3);
return 3;
case 3:
u.setImageResource(R.drawable.die4);
return 4;
case 4:
u.setImageResource(R.drawable.die5);
return 5;
case 5:
u.setImageResource(R.drawable.die6);
return 6;
default:
return 0;
}
}
return 0;
}
//The real logic of the game.
public void rollHandler() {
//Find the required textboxes. As they have the same ID in landscape and portrait, we can do it all in one go.
final TextView playerWinCountBox = (TextView) findViewById(R.id.playerWins);
final TextView houseWinCountBox = (TextView) findViewById(R.id.houseWins);
final TextView pointCountBox = (TextView) findViewById(R.id.pointbox);
final TextView rollTotalBox = (TextView) findViewById(R.id.rollTotal);
final TextView currentTotal = (TextView) findViewById(R.id.BankTotal);
//Call the roll method.
rollResults();
//Set the roll Total
rollTotalBox.setText("" + rollTotal);
//If this is the first time we're rolling, check if the player gets craps or wins, or if the game continues.
if (firstRoll == true) {
pointCountBox.setText("Point: ");
if (rollTotal == 7 || rollTotal == 11) {
playerWins++;
Toast.makeText(getApplicationContext(), "You won!", Toast.LENGTH_LONG).show();
playerWinCountBox.setText("Player Wins: " + playerWins);
currentTotalValue += betAmount*2;
currentTotal.setText("Current Total: $"+currentTotalValue+".00");
} else if (rollTotal == 2 || rollTotal == 3 || rollTotal == 12) {
houseWins++;
Toast.makeText(getApplicationContext(), "Better luck next time!", Toast.LENGTH_LONG).show();
houseWinCountBox.setText("House Wins: " + houseWins);
currentTotalValue -= betAmount * 2;
currentTotal.setText("Current Total: $"+currentTotalValue+".00");
if (currentTotalValue < betAmount)
{
decreaseBet(currentTotalValue);
}
} else {
pointValue = rollTotal;
firstRoll = false;
pointCountBox.setText("Point: " + pointValue);
}
}
//As the game continued, check if they get the 7, or if they win. If neither, the program just ends the method as it runs out of code.
else if (firstRoll == false) {
if (rollTotal == 7) {
houseWins++;
Toast.makeText(getApplicationContext(), "Better luck next time!", Toast.LENGTH_LONG).show();
houseWinCountBox.setText("House Wins: " + houseWins);
currentTotalValue -= betAmount;
currentTotal.setText("Current Total: $"+currentTotalValue+".00");
if (currentTotalValue < betAmount)
{
decreaseBet(currentTotalValue);
}
firstRoll = true;
} else if (rollTotal == pointValue) {
playerWins++;
Toast.makeText(getApplicationContext(), "You won!", Toast.LENGTH_LONG).show();
playerWinCountBox.setText("Player Wins: " + playerWins);
firstRoll = true;
currentTotalValue += betAmount;
currentTotal.setText("Current Total: $"+currentTotalValue+".00");
}
}
}
public void increaseBet() {
final TextView currentBet = (TextView) findViewById(R.id.betView);
if (betAmount + 1 <= currentTotalValue)
{
betAmount++;
currentBet.setText("$"+betAmount+".00");
}
else if (betAmount + 1 > currentTotalValue)
{
Toast.makeText(getApplicationContext(), "You can't bet more than you have!", Toast.LENGTH_LONG).show();
}
else if (betAmount + 1 > 50)
{
Toast.makeText(getApplicationContext(), "The maximum bet is $50", Toast.LENGTH_LONG).show();
}
}
public void decreaseBet(){
final TextView currentBet = (TextView) findViewById(R.id.betView);
if (betAmount - 1 >= 1)
{
betAmount--;
currentBet.setText("$"+betAmount+".00");
}
else if (betAmount - 1 <= 1)
{
Toast.makeText(getApplicationContext(), "The minimum bet is $1", Toast.LENGTH_LONG).show();
}
}
//Specific version of decreaseBet that will be used when the user loses so much that their current bet no longer is legal.
public void decreaseBet(int newBet){
final TextView currentBet = (TextView) findViewById(R.id.betView);
betAmount = newBet;
currentBet.setText("$"+betAmount+".00");
if (betAmount < 1)
{
setContentView(R.layout.home_screen);
Toast.makeText(getApplicationContext(), "I'm afraid you can no longer meet the minimum bet! Better luck next time!", Toast.LENGTH_LONG).show();
}
}
}
我道歉,我知道有很多代码需要注意,但如果有人能找到解决方案,我会非常感激〜!