Java Android应用程序发布时崩溃
因此,我必须开发一个应用程序来玩游戏垃圾,我必须修改它以允许主屏幕上有规则、买入系统和投注系统。在这次修改之前,应用程序工作时没有任何问题,但是在添加修改后,应用程序在我启动它的那一刻就崩溃了。在这一点上,我找不到任何修复它的方法,其他线程上列出的答案似乎不适用,所以我转向so寻求帮助。以下是我的全部代码: `Java Android应用程序发布时崩溃,java,android,Java,Android,因此,我必须开发一个应用程序来玩游戏垃圾,我必须修改它以允许主屏幕上有规则、买入系统和投注系统。在这次修改之前,应用程序工作时没有任何问题,但是在添加修改后,应用程序在我启动它的那一刻就崩溃了。在这一点上,我找不到任何修复它的方法,其他线程上列出的答案似乎不适用,所以我转向so寻求帮助。以下是我的全部代码: ` 废话! 试试你的运气! 2. 你的胜利:0 豪斯赢:0 滚! 你赢了! 要点: 祝你下次好运! 最低买入价为5.00美元,无任何变化! 最高买入价为$200.00,无变化! 最
废话!
试试你的运气!
2.
你的胜利:0
豪斯赢:0
滚!
你赢了!
要点:
祝你下次好运!
最低买入价为5.00美元,无任何变化!
最高买入价为$200.00,无变化!
最低赌注是1美元
最大赌注为50美元
第一次掷7或11,你赢了两倍的赌注!
掷2、3或12,你输了两倍的赌注!
掷其他任何东西,这就是你的重点!
再次掷骰得分获胜,但如果掷7分,则失败!
package com.example.kasresredux.akeelin3_groupassignment01;
导入android.support.v7.app.AppActivity;
导入android.os.Bundle;
导入android.view.view;
导入android.widget.Button;
导入android.widget.EditText;
导入android.widget.ImageView;
导入android.widget.TextView;
导入android.widget.Toast;
导入java.util.Random;
公共类CrapsGame扩展了AppCompatActivity{
//开发人员注意:因为它不是专门要求的,我也不希望人们在介绍中理解如何做到这一点
//类中,我没有为计算机提供任何保存屏幕旋转信息的方法,因此应用程序会停止并重新启动。
//创建随机函数和所需变量。它们在方法之外,因此可以在任何地方使用。
Random randomRoll=新的Random();
int DIERROLLONE,DIERROLLTWO=1;
int tempRoll;
int rollTotal=2;
布尔值为firstdie,firstRoll=true;
int playerWins=0;
int houseins=0;
int pointValue=0;
int buyinamount=0;
int-betAmount=1;
int currentTotalValue;
//onCreate方法,用于处理布局控制和以单独的方法处理卷。
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(右布局主屏幕);
最终按钮buyInButton=(按钮)findViewById(R.id.buyInButton);
//检查买入是否合法,如果合法,则继续游戏。
buyInButton.setOnClickListener(新视图.OnClickListener(){
公共void onClick(视图v){
最终EditText buyInCounter=(EditText)findViewById(R.id.InputAmount);
试一试{
buyinamount=Integer.parseInt(buyInCounter.toString());
}
捕获(数字格式){
}
如果(购买数量>=5.00)
{
currentTotalValue=Buyinamount;
Toast.makeText(getApplicationContext(),“欢迎,祝你好运!”,Toast.LENGTH\u LONG.show();
setContentView(R.layout.activity\u craps\u game);
}
else if(buyInCounter==null)
{
Toast.makeText(getApplicationContext(),“请输入您的买入金额!”,Toast.LENGTH\u LONG.show();
}
否则如果(购买数量500)
{
Toast.makeText(getApplicationContext(),“最高购买价为500美元!”,Toast.LENGTH\u LONG.show();
}
}
});
//这可能是不必要的,但我想确定一下。
if(getResources().getDisplayMetrics().widthPixelsgetResources().getDisplayMetrics().heightPixels){
最终按钮按钮roll=(按钮)findViewById(R.id.OrderButton);
buttonRoll.setOnClickListener(新视图.OnClickListener(){
公共void onClick(视图v){
rollHandler();
}
});
}
最终按钮递增BetButton=(按钮)findViewById(R.id.increaseBet);
最终按钮DecreasebetButton=(按钮)findViewById(R.id.lowerBet);
increaseBetButton.setOnClickListener(新视图.OnClickListener(){
公共void onClick(视图v){
递增bet();
}
});
descreaseBetButton.setOnClickListener(新视图.OnClickListener(){
公共void onClick(视图v){
减量();
}
});
}
//决定卷数的主要代码。
公共结果(){
//重置rollTotal,使其不能无限大。
rollTotal=0;
//确定这是两张骰子图像中的第一张。
isFirstDie=true;
tempRoll=randomRoll.nextInt(6);
dieRollOne=分色图像(tempRoll,isFirstDie);
//添加Dierroll结果,而不是tempRoll,因为tempRoll将比我们需要的低一个,并且我们不使用1d6-1。
rollTotal+=二烯烃;
isFirstDie=false;
tempRoll=randomRoll.nextInt(6);
dieRollTwo=decideImage(tempRoll,isFirstDie);
rollTotal+=dieRollTwo;
}
//用于更改模具图像的方法。
公共整数决策图像(整数滚动,布尔Dierolone){
//找到要更改的模具,并将该图像设置为它应该是的样子。遗憾的是,不会包含任何花哨的滚动动画。
ImageView i=(ImageView)findViewById(R.id.die1);
ImageView u=(ImageView)findViewById(R.id.die2);
如果(Dierolone==真){
开关(滚动){
案例0:
i、 setImageResource(R.drawable.die1);
返回1;
案例1:
i、 setImageResource(R.drawable.die2);
返回2;
案例2:
i、 setImageResource(R.drawable.die3);
<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();
}
}
}