gpt4 book ai didi

java - 我的应用程序在启动时停止

转载 作者:太空宇宙 更新时间:2023-11-04 14:23:45 25 4
gpt4 key购买 nike

所以我刚刚开始用java编程,并尝试制作一个TicTicToeGame,它成功了:)问题是,你只能玩1个TicTacToe游戏,所以我想制作一个重新启动游戏的按钮和一个按钮退出游戏。因为我添加了这个并尝试在我的手机上运行它,所以当我启动它时它崩溃了。这是我的 MainActivity.java:

package com.example.testjk;

import android.support.v7.app.ActionBarActivity;
import android.content.Context;
import android.graphics.Color;
import android.inputmethodservice.ExtractEditText;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;



public class MainActivity extends ActionBarActivity implements OnClickListener{

private TicTacToeGame mGame;

private Button mBoardButtons[];

private TextView mInfoTextView;
private TextView mHumanCount;
private TextView mTieCount;
private TextView mAndroidCount;

private int mHumanCounter = 0;
private int mTieCounter = 0;
private int mAndroidCounter = 0;

private boolean mHumanFirst = true;
private boolean mGameOver = false;




@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

mBoardButtons = new Button[mGame.getBOARD_SIZE()];
mBoardButtons[0] = (Button) findViewById(R.id.one);
mBoardButtons[1] = (Button) findViewById(R.id.two);
mBoardButtons[2] = (Button) findViewById(R.id.three);
mBoardButtons[3] = (Button) findViewById(R.id.four);
mBoardButtons[4] = (Button) findViewById(R.id.five);
mBoardButtons[5] = (Button) findViewById(R.id.six);
mBoardButtons[6] = (Button) findViewById(R.id.seven);
mBoardButtons[7] = (Button) findViewById(R.id.eight);
mBoardButtons[8] = (Button) findViewById(R.id.nine);

Button mTen = (Button) findViewById(R.id.ten);
mTen.setOnClickListener((OnClickListener) this);
Button mEleven = (Button) findViewById(R.id.eleven);
mEleven.setOnClickListener((OnClickListener) this);


mInfoTextView = (TextView) findViewById(R.id.information);
mHumanCount = (TextView) findViewById(R.id.humancount);
mTieCount = (TextView) findViewById(R.id.tiesCount);
mAndroidCount = (TextView) findViewById(R.id.androidCount);

mHumanCount.setText(Integer.toString(mHumanCounter));
mTieCount.setText(Integer.toString(mTieCounter));
mHumanCount.setText(Integer.toString(mAndroidCounter));

mGame = new TicTacToeGame();

startNewGame();


}

@Override
public void onClick(View v)
{
switch (v.getId())
{
case R.id.ten:

startNewGame();
break;

case R.id.eleven:

MainActivity.this.finish();
break;

}
}



private void startNewGame()
{
mGame.clearBoard();

for (int i = 0; i < mBoardButtons.length; i++)
{
mBoardButtons[i].setText("");
mBoardButtons[i].setEnabled(true);
mBoardButtons[i].setOnClickListener(new ButtonClickListener(i));

}
if (mHumanFirst)
{
mInfoTextView.setText(R.string.first_human);
mHumanFirst = false;
}
else
{
mInfoTextView.setText(R.string.turn_computer);
int move = mGame.getComputerMove();
setMove(mGame.ANDROID_PLAYER, move);
mHumanFirst = true;
}
}

private class ButtonClickListener implements View.OnClickListener
{
int location;

public ButtonClickListener(int location)
{
this.location = location;
}

public void onClick(View view)
{
if (!mGameOver)
{
if(mBoardButtons[location].isEnabled())
{
setMove(mGame.HUMAN_PLAYER, location);

int winner = mGame.checkForWinner();

if (winner == 0)
{
mInfoTextView.setText(R.string.turn_computer);
int move = mGame.getComputerMove();
setMove(mGame.ANDROID_PLAYER, move);
winner = mGame.checkForWinner();

}
if (winner == 0)
mInfoTextView.setText(R.string.turn_human);
else if (winner == 1)
{
mInfoTextView.setText(R.string.result_tie);
mTieCounter++;
mTieCount.setText(Integer.toString(mTieCounter));
mGameOver = true;
}
else if (winner ==2)
{
mInfoTextView.setText(R.string.result_human_wins);
mHumanCounter++;
mHumanCount.setText(Integer.toString(mHumanCounter));
mGameOver = true;
}
else if (winner ==3)
{
mInfoTextView.setText(R.string.result_android_wins);
mAndroidCounter++;
mAndroidCount.setText(Integer.toString(mAndroidCounter));
mGameOver = true;
}
}
}
}
}

private void setMove(char player, int location)
{
mGame.setMove(player,location);
mBoardButtons[location].setEnabled(false);
mBoardButtons[location].setText(String.valueOf(player));
if (player == mGame.HUMAN_PLAYER)
mBoardButtons[location].setTextColor(Color.GREEN);
else
{
mBoardButtons[location].setTextColor(Color.RED);
}
}

}

这是我的 strings.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>

<string name="app_name">testjk</string>
<string name="hello_world">Hello world!</string>
<string name="turn_human">Jouw beurt.</string>
<string name="first_human">Jij mag eerst beginnen.</string>
<string name="turn_computer">computer is aan het nadenken</string>
<string name="result_tie">Gelijkspel!</string>
<string name="result_human_wins">net aan gewonnen!</string>
<string name="result_android_wins">sukkel!</string>
<string name="one">1</string>
<string name="two">2</string>
<string name="three">3</string>
<string name="four">4</string>
<string name="five">5</string>
<string name="six">6</string>
<string name="seven">7</string>
<string name="eight">8</string>
<string name="nine">9</string>
<string name="ten">nieuw spel</string>
<string name="eleven">stoppen</string>
<string name="info">Info</string>
<string name="human">JIJ: </string>
<string name="ties">Gelijkspel: </string>
<string name="android">Computer: </string>
<string name="contact_heading">Contact Informatie</string>
<string name="action_settings">Settings</string>
<string name="contact_info">

<b>Name:</b> Wouter\n
<b>Achternaam</b> de Jong\n
<b>Email</b> woutter@live.nl
</string>
<string name="about_heading">over </string>
<string name="TicTacToeGame">Boter Kaas en Eieren van wouter de jong</string>


</resources>

这是我的activity_main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="26dp"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.testjk.MainActivity" >

<TableLayout
android:id="@+id/PlayArea"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="10dp" >


<TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="clip_horizontal" >

<Button
android:id="@+id/one"
android:layout_width="100dp"
android:layout_height="100dp"
android:text="@string/one"
android:textSize="70dp" />

<Button
android:id="@+id/two"
android:layout_width="100dp"
android:layout_height="100dp"
android:text="@string/two"
android:textSize="70dp" />

<Button
android:id="@+id/three"
android:layout_width="100dp"
android:layout_height="100dp"
android:text="@string/three"
android:textSize="70dp" />
</TableRow>

<TableRow
android:id="@+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="clip_horizontal" >

<Button
android:id="@+id/four"
android:layout_width="100dp"
android:layout_height="100dp"
android:text="@string/four"
android:textSize="70dp" />

<Button
android:id="@+id/five"
android:layout_width="100dp"
android:layout_height="100dp"
android:text="@string/five"
android:textSize="70dp" />

<Button
android:id="@+id/six"
android:layout_width="100dp"
android:layout_height="100dp"
android:text="@string/six"
android:textSize="70dp" />
</TableRow>

<TableRow
android:id="@+id/tableRow3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="clip_horizontal" >

<Button
android:id="@+id/seven"
android:layout_width="100dp"
android:layout_height="100dp"
android:text="@string/seven"
android:textSize="70dp" />

<Button
android:id="@+id/eight"
android:layout_width="100dp"
android:layout_height="100dp"
android:text="@string/eight"
android:textSize="70dp" />

<Button
android:id="@+id/nine"
android:layout_width="100dp"
android:layout_height="100dp"
android:text="@string/nine"
android:textSize="70dp" />
</TableRow>
</TableLayout>

<TableRow
android:id="@+id/tableRow4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/information"
android:layout_centerHorizontal="true"
android:gravity="center_horizontal|clip_horizontal|fill_horizontal" >

<TextView
android:id="@+id/human"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/human" />

<TextView
android:id="@+id/humancount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp" />

<TextView
android:id="@+id/ties"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/ties" />

<TextView
android:id="@+id/tiesCount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp" />

<TextView
android:id="@+id/androidcount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/android" />

<TextView
android:id="@+id/androidCount"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</TableRow>

<TextView
android:id="@+id/information"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/PlayArea"
android:layout_below="@+id/PlayArea"
android:gravity="center"
android:text="@string/info"
android:textAppearance="?android:attr/textAppearanceLarge" />

<TableLayout
android:id="@+id/tableLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/tableRow4"
android:layout_marginTop="32dp" >

<TableRow
android:id="@+id/tableRow5"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >

<Button
android:id="@+id/ten"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:text="@string/ten" />

<Button
android:id="@+id/eleven"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:text="@string/eleven" />

</TableRow>
</TableLayout>

</RelativeLayout>

TicTacToeGame.java:

package com.example.testjk;

import java.util.Random;

public class TicTacToeGame {

private char mBoard[];
private final static int BOARD_SIZE = 9;

public static final char HUMAN_PLAYER = 'X';
public static final char ANDROID_PLAYER = '0';
public static final char EMPTY_SPACE = ' ';

private Random mRand;

public static int getBOARD_SIZE() {
return BOARD_SIZE;
}

public TicTacToeGame(){
mBoard = new char[BOARD_SIZE];

for (int i = 0; i < BOARD_SIZE; i++)
mBoard[i] = EMPTY_SPACE;

mRand = new Random();
}

public void clearBoard()
{
for (int i = 0;i < BOARD_SIZE; i++)
{
mBoard[i] = EMPTY_SPACE;
}
}

public void setMove(char player, int location)
{
mBoard[location] = player;
}

public int getComputerMove()
{
int move;

for (int i = 0; i < getBOARD_SIZE(); i++)
{
if (mBoard[i] != HUMAN_PLAYER && mBoard[i] != ANDROID_PLAYER)
{
char curr = mBoard[i];
mBoard[i] = ANDROID_PLAYER;
if (checkForWinner() == 3)
{
setMove(ANDROID_PLAYER, i);
return i;
}
else
mBoard[i] = curr;
}
}
for (int i = 0; i < getBOARD_SIZE(); i++)
{
if (mBoard[i] != HUMAN_PLAYER && mBoard[i] != ANDROID_PLAYER)
{
char curr = mBoard[i];
mBoard[i] = HUMAN_PLAYER;
if (checkForWinner() == 2)
{
setMove(ANDROID_PLAYER, i);
return i;
}
else
mBoard[i] = curr;
}
}
do
{
move = mRand.nextInt(getBOARD_SIZE());
}while (mBoard[move]==HUMAN_PLAYER || mBoard[move] == ANDROID_PLAYER);

setMove(ANDROID_PLAYER, move);
return move;
}
public int checkForWinner()
{
for (int i=0;i<=6;i+=3)
{
if (mBoard[i] == HUMAN_PLAYER &&
mBoard[i+1] == HUMAN_PLAYER &&
mBoard[i+2] == HUMAN_PLAYER)
return 2;
if (mBoard[i] == ANDROID_PLAYER &&
mBoard[i+1] == ANDROID_PLAYER &&
mBoard[i+2] == ANDROID_PLAYER)
return 3;
}
for (int i = 0; i <=2; i++)
{
if (mBoard[i] == HUMAN_PLAYER &&
mBoard[i+3] == HUMAN_PLAYER &&
mBoard[i+6] == HUMAN_PLAYER)
return 2;
if (mBoard[i] == ANDROID_PLAYER &&
mBoard[i+3] == ANDROID_PLAYER &&
mBoard[i+6] == ANDROID_PLAYER)
return 3;

}
if((mBoard[0] == HUMAN_PLAYER &&
mBoard[4] == HUMAN_PLAYER &&
mBoard[8] == HUMAN_PLAYER)||
(mBoard[2] == HUMAN_PLAYER &&
mBoard[4] == HUMAN_PLAYER &&
mBoard[6] == HUMAN_PLAYER))
return 2;
if((mBoard[0] == ANDROID_PLAYER &&
mBoard[4] == ANDROID_PLAYER &&
mBoard[8] == ANDROID_PLAYER)||
(mBoard[2] == ANDROID_PLAYER &&
mBoard[4] == ANDROID_PLAYER &&
mBoard[6] == ANDROID_PLAYER))
return 3;


for (int i = 0; i < getBOARD_SIZE(); i++)
{
if (mBoard[i] != HUMAN_PLAYER && mBoard[i] != ANDROID_PLAYER)
return 0;
}
return 1;
}
}

我的 bin/res 文件夹中的 AndroidManiFest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.testjk"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="21" />

<application
android:allowBackup="true"
android:icon="@drawable/ic_hoi"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>

game_menu.xml 在我的 res/menu 文件夹中:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >





</menu>

这些是我编辑的所有类(class)。这是我得到的 logcat:/image/vP6De.png

最佳答案

package com.example.testjk;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity implements OnClickListener{

private TicTacToeGame mGame;

private Button mBoardButtons[];

private TextView mInfoTextView;
private TextView mHumanCount;
private TextView mTieCount;
private TextView mAndroidCount;

private int mHumanCounter = 0;
private int mTieCounter = 0;
private int mAndroidCounter = 0;

private boolean mHumanFirst = true;
private boolean mGameOver = false;




@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

mBoardButtons = new Button[mGame.getBOARD_SIZE()];
mBoardButtons[0] = (Button) findViewById(R.id.one);
mBoardButtons[1] = (Button) findViewById(R.id.two);
mBoardButtons[2] = (Button) findViewById(R.id.three);
mBoardButtons[3] = (Button) findViewById(R.id.four);
mBoardButtons[4] = (Button) findViewById(R.id.five);
mBoardButtons[5] = (Button) findViewById(R.id.six);
mBoardButtons[6] = (Button) findViewById(R.id.seven);
mBoardButtons[7] = (Button) findViewById(R.id.eight);
mBoardButtons[8] = (Button) findViewById(R.id.nine);

Button mTen = (Button) findViewById(R.id.ten);
mTen.setOnClickListener((OnClickListener) this);
Button mEleven = (Button) findViewById(R.id.eleven);
mEleven.setOnClickListener((OnClickListener) this);


mInfoTextView = (TextView) findViewById(R.id.information);
mHumanCount = (TextView) findViewById(R.id.humancount);
mTieCount = (TextView) findViewById(R.id.tiesCount);
mAndroidCount = (TextView) findViewById(R.id.androidCount);

mHumanCount.setText(Integer.toString(mHumanCounter));
mTieCount.setText(Integer.toString(mTieCounter));
mHumanCount.setText(Integer.toString(mAndroidCounter));

mGame = new TicTacToeGame();

startNewGame();


}

@Override
public void onClick(View v)
{
switch (v.getId())
{
case R.id.ten:

startNewGame();
break;

case R.id.eleven:

MainActivity.this.finish();
break;

}
}



private void startNewGame()
{
mGame.clearBoard();

for (int i = 0; i < mBoardButtons.length; i++)
{
mBoardButtons[i].setText("");
mBoardButtons[i].setEnabled(true);
mBoardButtons[i].setOnClickListener(new ButtonClickListener(i));

}
if (mHumanFirst)
{
mInfoTextView.setText(R.string.first_human);
mHumanFirst = false;
}
else
{
mInfoTextView.setText(R.string.turn_computer);
int move = mGame.getComputerMove();
setMove(mGame.ANDROID_PLAYER, move);
mHumanFirst = true;
}
}

private class ButtonClickListener implements View.OnClickListener
{
int location;

public ButtonClickListener(int location)
{
this.location = location;
}

public void onClick(View view)
{
if (!mGameOver)
{
if(mBoardButtons[location].isEnabled())
{
setMove(mGame.HUMAN_PLAYER, location);

int winner = mGame.checkForWinner();

if (winner == 0)
{
mInfoTextView.setText(R.string.turn_computer);
int move = mGame.getComputerMove();
setMove(mGame.ANDROID_PLAYER, move);
winner = mGame.checkForWinner();

}
if (winner == 0)
mInfoTextView.setText(R.string.turn_human);
else if (winner == 1)
{
mInfoTextView.setText(R.string.result_tie);
mTieCounter++;
mTieCount.setText(Integer.toString(mTieCounter));
mGameOver = true;
}
else if (winner ==2)
{
mInfoTextView.setText(R.string.result_human_wins);
mHumanCounter++;
mHumanCount.setText(Integer.toString(mHumanCounter));
mGameOver = true;
}
else if (winner ==3)
{
mInfoTextView.setText(R.string.result_android_wins);
mAndroidCounter++;
mAndroidCount.setText(Integer.toString(mAndroidCounter));
mGameOver = true;
}
}
}
}
}

private void setMove(char player, int location)
{
mGame.setMove(player,location);
mBoardButtons[location].setEnabled(false);
mBoardButtons[location].setText(String.valueOf(player));
if (player == mGame.HUMAN_PLAYER)
mBoardButtons[location].setTextColor(Color.GREEN);
else
{
mBoardButtons[location].setTextColor(Color.RED);
}
}



}

它在这里工作,因为我不使用appcompact 7进行开发,我使用 Activity 来扩展你的类,并且我也没有使用你的menu.xml,首先删除该文件并尝试构建你的代码,如果这不起作用使用这个文件在我这边工作正常,游戏重新启动和应用程序关闭也可以。谢谢 使用这个并在出现任何问题时返回。在这里查看它的工作enter image description here

关于java - 我的应用程序在启动时停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26894869/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com