我有此代码用于tictactoe android应用
XSI0类
package xsi0.cristi.ro;
import java.util.Random;
public class XSI0 {
private char Tabla[];
private final static int MarimeTabla=9;
public final char User='X';
public final char Android='0';
public final char SpatiuGol= ' ';
private Random mRand;
public int getMarimeTabla(){
return MarimeTabla;
}
public XSI0(){
Tabla=new char[MarimeTabla];
for(int i = 0; i < MarimeTabla; i++)
Tabla[i]=SpatiuGol;
mRand=new Random();
}
public void curataTabla(){
for(int i=0;i<MarimeTabla;i++)
{
Tabla[i]=SpatiuGol;
}
}
public void setMiscare(char jucator,int locatie){
Tabla[locatie]=jucator;
}
public int getMiscareComputer(){
int miscare;
for(int i=0; i<getMarimeTabla();i++){
if(Tabla[i]!=User && Tabla[i]!=Android){
char curr=Tabla[i];
Tabla[i]=Android;
if(verificaCastigator()==3){
setMiscare(Android,i);
return i;
}
else
Tabla[i]=curr;
}
}
for(int i=0; i<getMarimeTabla();i++){
if(Tabla[i]!=User && Tabla[i]!=Android){
char curr=Tabla[i];
Tabla[i]=User;
if(verificaCastigator()==2){
setMiscare(Android,i);
return i;
}
else
Tabla[i]=curr;
}
}
do{
miscare=mRand.nextInt(getMarimeTabla());
}while(Tabla[miscare]==User || Tabla[miscare]==Android);
setMiscare(Android,miscare);
return miscare;
}
public int verificaCastigator(){
for(int i=0;i<=6;i++){//victorie orizontala
if(Tabla[i]==User &&
Tabla[i+1]==User &&
Tabla[i+2]==User)
return 2;//castiga jucator
}
for(int i=0;i<=6;i++){
if(Tabla[i]==Android &&
Tabla[i+1]==Android &&
Tabla[i+2]==Android)
return 3;//castiga android
}
for(int i=0;i<=2;i++){//victorie verticala
if(Tabla[i]==User &&
Tabla[i+3]==User &&
Tabla[i+6]==User)
return 2;//castiga jucator
}
for(int i=0;i<=2;i++){
if(Tabla[i]==Android &&
Tabla[i+3]==Android &&
Tabla[i+6]==Android)
return 3;//castiga android
}
if(Tabla[0]==User &&//victorie diagonala
Tabla[4]==User &&
Tabla[8]==User ||
Tabla[2]==User &&
Tabla[4]==User &&
Tabla[6]==User)
return 2;//castiga jucator
if(Tabla[0]==User &&//victorie diagonala
Tabla[4]==Android &&
Tabla[8]==Android ||
Tabla[2]==Android &&
Tabla[4]==Android &&
Tabla[6]==Android)
return 3;//castiga android
for(int i=0;i<getMarimeTabla();i++)
{
if(Tabla[i]!=User && Tabla[i]!=Android)
return 0;//jocul nu s-a terminat
}
return 1;//remiza
}
}
主要活动
package xsi0.cristi.ro;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.Color;
//import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
//import android.view.*;
public class MainActivity extends Activity {
private XSI0 mGame;
private Button[] mBoardsButtons;
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);
mBoardsButtons = new Button[mGame.getMarimeTabla()];
mBoardsButtons[0]= (Button)findViewById(R.id.unu);
mBoardsButtons[1]= (Button)findViewById(R.id.doi);
mBoardsButtons[2]= (Button)findViewById(R.id.trei);
mBoardsButtons[3]= (Button)findViewById(R.id.patru);
mBoardsButtons[4]= (Button)findViewById(R.id.cinci);
mBoardsButtons[5]= (Button)findViewById(R.id.sase);
mBoardsButtons[6]= (Button)findViewById(R.id.sapte);
mBoardsButtons[7]= (Button)findViewById(R.id.opt);
mBoardsButtons[8]= (Button)findViewById(R.id.noua);
mInfoTextView=(TextView)findViewById(R.id.Info);
mHumanCount=(TextView)findViewById(R.id.uwins);
mTieCount=(TextView)findViewById(R.id.Drawscount);
mAndroidCount=(TextView)findViewById(R.id.defeatscount);
mHumanCount.setText(Integer.toString(mHumanCounter));
mTieCount.setText(Integer.toString(mTieCounter));
mAndroidCount.setText(Integer.toString(mAndroidCounter));
mGame=new XSI0();
incepeJocNou();
}
private void incepeJocNou(){
mGame.curataTabla();
for(int i=0;i<mBoardsButtons.length;i++){
mBoardsButtons[i].setText("");
mBoardsButtons[i].setEnabled(true);
mBoardsButtons[i].setOnClickListener(new ButtonClickListener(i));
}
if(mHumanFirst){
mInfoTextView.setText(R.string.Utilizatorul_primul);
mHumanFirst=false;
}
else{
mInfoTextView.setText(R.string.mutare_android);
int move=mGame.getMiscareComputer();
setMove(mGame.Android,move);
mHumanFirst=true;
}
}
private void setMove(char player,int location){
mGame.setMiscare(player, location);
mBoardsButtons[location].setEnabled(false);
mBoardsButtons[location].setText(String.valueOf(player));
if(player==mGame.User){
mBoardsButtons[location].setTextColor(Color.BLUE);
}
else
mBoardsButtons[location].setTextColor(Color.RED);
}
private class ButtonClickListener implements View.OnClickListener{
int location;
public ButtonClickListener(int location){
this.location=location;
}
@Override
public void onClick(View view) {
if(!mGameOver){
if(mBoardsButtons[location].isEnabled()){
setMove(mGame.User,location);
int winner=mGame.verificaCastigator();
if(winner==0)
{
mInfoTextView.setText(R.string.mutare_android);
int move=mGame.getMiscareComputer();
setMove(mGame.Android,move);
winner=mGame.verificaCastigator();
}
if(winner==0)
mInfoTextView.setText(R.string.mutare_utilizator);
else if(winner==1){
mInfoTextView.setText(R.string.rezultat_egal);
mTieCounter++;
//mTieCounter.setText(Integer.toString(mTieCounter));
mGameOver=true;
}
else if(winner==2){
mInfoTextView.setText(R.string.utilizator_castiga);
mHumanCounter++;
//mHumanCounter.setText(Integer.toString(mHumanCounter));
mGameOver=true;
}
else{
mInfoTextView.setText(R.string.android_castiga);
mAndroidCounter++;
//mAndroidCounter.setText(Integer.toString(mAndroidCounter));
mGameOver=true;
}
}
}
}
/*public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}*/
}
}
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/Information"
android:layout_width="match_parent"
android:layout_height="match_parent"
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=".MainActivity" >
<TableLayout
android:id="@+id/PlayArea"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true" >
<TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button
android:id="@+id/unu"
android:layout_width="100dp"
android:layout_height="100dp"
android:text="@string/unu"
android:textSize="80sp" />
<Button
android:id="@+id/doi"
android:layout_width="100dp"
android:layout_height="100dp"
android:text="@string/doi"
android:textSize="80sp" />
<Button
android:id="@+id/trei"
android:layout_width="100dp"
android:layout_height="100dp"
android:text="@string/trei"
android:textSize="80sp" />
</TableRow>
<TableRow
android:id="@+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button
android:id="@+id/patru"
android:layout_width="100dp"
android:layout_height="100dp"
android:text="@string/patru"
android:textSize="80sp" />
<Button
android:id="@+id/cinci"
android:layout_width="100dp"
android:layout_height="100dp"
android:text="@string/cinci"
android:textSize="80sp" />
<Button
android:id="@+id/sase"
android:layout_width="100dp"
android:layout_height="100dp"
android:text="@string/sase"
android:textSize="80sp" />
</TableRow>
<TableRow
android:id="@+id/tableRow3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button
android:id="@+id/sapte"
android:layout_width="100dp"
android:layout_height="100dp"
android:text="@string/sapte"
android:textSize="80sp" />
<Button
android:id="@+id/opt"
android:layout_width="100dp"
android:layout_height="100dp"
android:text="@string/opt"
android:textSize="80sp" />
<Button
android:id="@+id/noua"
android:layout_width="100dp"
android:layout_height="100dp"
android:text="@string/noua"
android:textSize="80sp" />
</TableRow>
</TableLayout>
<TextView
android:id="@+id/Info"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/PlayArea"
android:layout_marginTop="18dp"
android:gravity="center_horizontal"
android:text="@string/info"
android:textSize="25sp" />
<TableLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/Info"
android:layout_alignRight="@+id/Info"
android:layout_below="@+id/Info"
android:layout_marginTop="24dp" >
<TableRow
android:id="@+id/tableRaw4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal" >
<TextView
android:id="@+id/w"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Victorii" />
<TextView
android:id="@+id/uwins"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp" />
<TextView
android:id="@+id/Draws"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Egaluri" />
<TextView
android:id="@+id/Drawscount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp" />
<TextView
android:id="@+id/defeats"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Infrangeri" />
<TextView
android:id="@+id/defeatscount"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</TableRow>
<TableRow
android:id="@+id/tableRow4"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</TableRow>
</TableLayout>
我得到这个错误:
01-03 00:47:36.503: E/AndroidRuntime(831): FATAL EXCEPTION: main
01-03 00:47:36.503: E/AndroidRuntime(831): java.lang.RuntimeException: Unable to start activity ComponentInfo{xsi0.cristi.ro/xsi0.cristi.ro.MainActivity}: java.lang.NullPointerException
01-03 00:47:36.503: E/AndroidRuntime(831): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
01-03 00:47:36.503: E/AndroidRuntime(831): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
01-03 00:47:36.503: E/AndroidRuntime(831): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
01-03 00:47:36.503: E/AndroidRuntime(831): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
01-03 00:47:36.503: E/AndroidRuntime(831): at android.os.Handler.dispatchMessage(Handler.java:99)
01-03 00:47:36.503: E/AndroidRuntime(831): at android.os.Looper.loop(Looper.java:123)
01-03 00:47:36.503: E/AndroidRuntime(831): at android.app.ActivityThread.main(ActivityThread.java:3683)
01-03 00:47:36.503: E/AndroidRuntime(831): at java.lang.reflect.Method.invokeNative(Native Method)
01-03 00:47:36.503: E/AndroidRuntime(831): at java.lang.reflect.Method.invoke(Method.java:507)
01-03 00:47:36.503: E/AndroidRuntime(831): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
01-03 00:47:36.503: E/AndroidRuntime(831): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-03 00:47:36.503: E/AndroidRuntime(831): at dalvik.system.NativeStart.main(Native Method)
01-03 00:47:36.503: E/AndroidRuntime(831): Caused by: java.lang.NullPointerException
01-03 00:47:36.503: E/AndroidRuntime(831): at xsi0.cristi.ro.MainActivity.onCreate(MainActivity.java:37)
01-03 00:47:36.503: E/AndroidRuntime(831): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-03 00:47:36.503: E/AndroidRuntime(831): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
01-03 00:47:36.503: E/AndroidRuntime(831): ... 11 more
当我尝试启动该应用程序时,它显示“应用程序意外停止”。
我遵循了youtube的tictactoe教程。
最佳答案
似乎您尚未在调用mGame
之前初始化getMarimeTabla()
。将行mGame=new XSI0();
移动到在其上调用getMarimeTabla()
之前的位置
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mGame=new XSI0(); // initialize the variable first
mBoardsButtons = new Button[mGame.getMarimeTabla()];