这是android应用程序的简单代码--GoBang。但是,我无法解决这个错误,因为我是一个新生。当应用程序跳转到这个活动时,它会立即崩溃。这是活动代码:
package org.crazyit.gobang;
import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.AttributeSet;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;
public class enter extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
GobangView gbv; //= new GobangView(this);
setContentView(R.layout.acyivity_enter);
gbv = (GobangView)this.findViewById(R.id.gobangview);
gbv.setTextView((TextView)this.findViewById(R.id.text));
}
public class GobangView extends View{
protected int GRID_SIZE = 10;
protected int GRID_WIDTH = 30;
protected int CHESS_DIAMETER = 26;
protected int mStartX;
protected int mStartY;
private Bitmap[] mChessBW;
private int[][] mGridArray;
boolean key = false;
int wbflag = 1;
int mLevel = 1;
int mWinFlag = 0;
private final int BLACK=1;
private final int WHITE=2;
int mGameState = GAMESTATE_RUN;
static final int GAMESTATE_PRE = 0;
static final int GAMESTATE_RUN = 1;
static final int GAMESTATE_PAUSE = 2;
static final int GAMESTATE_END = 3;
//private TextView mStatusTextView;
public TextView mStatusTextView;
private Bitmap btm1;
private final Paint mPaint = new Paint();
CharSequence mText;
CharSequence STRING_WIN = "White win! /n Press Fire Key to start new game.";
CharSequence STRING_LOSE = "Black win! /n Press Fire Key to start new game.";
CharSequence STRING_EQUAL = "Cool! You are equal! /n Press Fire Key to start new Game.";
public GobangView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public GobangView(Context context, AttributeSet attrs) {
super(context, attrs);
this.setFocusable(true); //20090530
this.setFocusableInTouchMode(true);
init();
}
public void init() {
mGameState = 1;
wbflag = BLACK;
mWinFlag = 0;
mGridArray = new int[GRID_SIZE-1][GRID_SIZE-1];
mChessBW = new Bitmap[2];
Bitmap bitmap = Bitmap.createBitmap(CHESS_DIAMETER, CHESS_DIAMETER, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
Resources r = this.getContext().getResources();
Drawable tile = r.getDrawable(R.drawable.bai);
tile.setBounds(0, 0, CHESS_DIAMETER, CHESS_DIAMETER);
tile.draw(canvas);
mChessBW[0] = bitmap;
tile = r.getDrawable(R.drawable.hei);
tile.setBounds(0, 0, CHESS_DIAMETER, CHESS_DIAMETER);
tile.draw(canvas);
mChessBW[1] = bitmap;
}
public void setTextView(TextView tv){
mStatusTextView =tv;
mStatusTextView.setVisibility(View.INVISIBLE);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
mStartX = w / 2 - GRID_SIZE * GRID_WIDTH / 2;
mStartY = h / 2 - GRID_SIZE * GRID_WIDTH / 2;
}
@Override
public boolean onTouchEvent(MotionEvent event){
switch (mGameState) {
case GAMESTATE_PRE:
break;
case GAMESTATE_RUN: {
int x;
int y;
float x0 = GRID_WIDTH - (event.getX() - mStartX) % GRID_WIDTH;
float y0 = GRID_WIDTH - (event.getY() - mStartY) % GRID_WIDTH;
if (x0 < GRID_WIDTH / 2) {
x = (int) ((event.getX() - mStartX) / GRID_WIDTH);
} else {
x = (int) ((event.getX() - mStartX) / GRID_WIDTH) - 1;
}
if (y0 < GRID_WIDTH / 2) {
y = (int) ((event.getY() - mStartY) / GRID_WIDTH);
} else {
y = (int) ((event.getY() - mStartY) / GRID_WIDTH) - 1;
}
if ((x >= 0 && x < GRID_SIZE - 1)
&& (y >= 0 && y < GRID_SIZE - 1)) {
if (mGridArray[x][y] == 0) {
if (wbflag == BLACK) {
putChess(x, y, BLACK);
//this.mGridArray[x][y] = 1;
if(checkWin(BLACK)){ //如果是黑棋赢了
mText = STRING_LOSE;
mGameState = GAMESTATE_END;
showTextView(mText);
}else if(checkFull()){//如果棋盘满了
mText = STRING_EQUAL;
mGameState = GAMESTATE_END;
showTextView(mText);
}
wbflag = WHITE;
} else if (wbflag == WHITE) {
putChess(x, y, WHITE);
//this.mGridArray[x][y] = 2;
if(checkWin(WHITE)){
mText = STRING_WIN;
mGameState = GAMESTATE_END;
showTextView(mText);
}else if(checkFull()){//如果棋盘满了
mText = STRING_EQUAL;
mGameState = GAMESTATE_END;
showTextView(mText);
}
wbflag = BLACK;
}
}
}
}
break;
case GAMESTATE_PAUSE:
break;
case GAMESTATE_END:
break;
}
this.invalidate();
return true;
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent msg) {
Log.e("KeyEvent.KEYCODE_DPAD_CENTER", " " + keyCode);
if(keyCode == KeyEvent.KEYCODE_DPAD_CENTER){
switch(mGameState){
case GAMESTATE_PRE:
break;
case GAMESTATE_RUN:
break;
case GAMESTATE_PAUSE:
break;
case GAMESTATE_END:
{//游戏结束后,按CENTER键继续
Log.e("Fire Key Pressed:::", "FIRE");
mGameState = GAMESTATE_RUN;
this.setVisibility(View.VISIBLE);
this.mStatusTextView.setVisibility(View.INVISIBLE);
this.init();
this.invalidate();
}
break;
}
}
return super.onKeyDown(keyCode, msg);
}
@Override
public void onDraw(Canvas canvas) {
canvas.drawColor(Color.YELLOW);
// 画棋盘
{
Paint paintRect = new Paint();
paintRect.setColor(Color.GRAY);
paintRect.setStrokeWidth(2);
paintRect.setStyle(Style.STROKE);
for (int i = 0; i < GRID_SIZE; i++) {
for (int j = 0; j < GRID_SIZE; j++) {
int mLeft = i * GRID_WIDTH + mStartX;
int mTop = j * GRID_WIDTH + mStartY;
int mRright = mLeft + GRID_WIDTH;
int mBottom = mTop + GRID_WIDTH;
canvas.drawRect(mLeft, mTop, mRright, mBottom, paintRect);
}
}
//画棋盘的外边框
paintRect.setStrokeWidth(4);
canvas.drawRect(mStartX, mStartY, mStartX + GRID_WIDTH*GRID_SIZE, mStartY + GRID_WIDTH*GRID_SIZE, paintRect);
}
//画棋子
for (int i = 0; i < GRID_SIZE-1; i++) {
for (int j = 0; j < GRID_SIZE-1; j++) {
if(mGridArray[i][j] == BLACK){
//通过图片来画
//canvas.drawBitmap(mChessBW[0], mStartX + (i+1) * GRID_WIDTH - CHESS_DIAMETER/2 , mStartY + (j+1)* GRID_WIDTH - CHESS_DIAMETER/2 , mPaint);
//通过圆形来画
{
Paint paintCircle = new Paint();
paintCircle.setColor(Color.BLACK);
canvas.drawCircle(mStartX + (i+1) * GRID_WIDTH, mStartY + (j+1)* GRID_WIDTH, CHESS_DIAMETER/2, paintCircle);
}
}else if(mGridArray[i][j] == WHITE){
//通过图片来画
//canvas.drawBitmap(mChessBW[1], mStartX + (i+1) * GRID_WIDTH - CHESS_DIAMETER/2 , mStartY + (j+1)* GRID_WIDTH - CHESS_DIAMETER/2 , mPaint);
//通过圆形来画
{
Paint paintCircle = new Paint();
paintCircle.setColor(Color.WHITE);
canvas.drawCircle(mStartX + (i+1) * GRID_WIDTH, mStartY + (j+1)* GRID_WIDTH, CHESS_DIAMETER/2, paintCircle);
}
}
}
}
}
public void putChess(int x, int y, int blackwhite){
mGridArray[x][y] = blackwhite;
}
public boolean checkWin(int wbflag){
for(int i = 0; i < GRID_SIZE - 1 ; i++ ) //i表示列(根据宽度算出来的)
for(int j = 0; j < GRID_SIZE - 1; j++){//i表示行(根据高度算出来的)
//检测横轴五个相连
if(((i+4) < (GRID_SIZE - 1))&&
(mGridArray[i][j] == wbflag) && (mGridArray[i+1][j] == wbflag)&& (mGridArray[i + 2][j] == wbflag) && (mGridArray[i + 3][j] == wbflag) && (mGridArray[i + 4][j] == wbflag)){
Log.e("check win or loss:", wbflag + "win");
mWinFlag = wbflag;
}
//纵轴5个相连
if(((j+4) < (GRID_SIZE - 1))&&
(mGridArray[i][j] == wbflag) && (mGridArray[i][j+1] == wbflag)&& (mGridArray[i ][j+ 2] == wbflag) && (mGridArray[i ][j+ 3] == wbflag) && (mGridArray[i ][j+ 4] == wbflag)){
Log.e("check win or loss:", wbflag + "win");
mWinFlag = wbflag;
}
//左上到右下5个相连
if(((j+4) < (GRID_SIZE - 1))&& ((i+4) < (GRID_SIZE - 1)) &&
(mGridArray[i][j] == wbflag) && (mGridArray[i+1][j+1] == wbflag)&& (mGridArray[i + 2 ][j+ 2] == wbflag) && (mGridArray[i + 3][j+ 3] == wbflag) && (mGridArray[i + 4 ][j+ 4] == wbflag)){
Log.e("check win or loss:", wbflag + "win");
mWinFlag = wbflag;
}
//右上到左下5个相连
if(((i-4) >= 0)&& ((j+4) < (GRID_SIZE - 1)) &&
(mGridArray[i][j] == wbflag) && (mGridArray[i-1][j+1] == wbflag)&& (mGridArray[i - 2 ][j+ 2] == wbflag) && (mGridArray[i - 3][j+ 3] == wbflag) && (mGridArray[i - 4 ][j+ 4] == wbflag)){
Log.e("check win or loss:", wbflag + "win");
mWinFlag = wbflag;
}
}
if( mWinFlag == wbflag){
return true;
}else
return false;
}
public boolean checkFull(){
int mNotEmpty = 0;
for(int i = 0; i < GRID_SIZE -1; i ++)
for(int j = 0; j < GRID_SIZE - 1; j ++){
if(mGridArray[i][j] != 0) mNotEmpty +=1;
}
if(mNotEmpty == (GRID_SIZE-1)*(GRID_SIZE-1)) return true;
else return false;
}
public void showTextView(CharSequence mT){
this.mStatusTextView.setText(mT);
mStatusTextView.setVisibility(View.VISIBLE);
}
}
}
这是xml代码:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<org.crazyit.gobang.GobangView android:id="@+id/gobangview"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" >
<TextView
android:id="@+id/text"
android:text="hahahhaha"
android:visibility="visible"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center_horizontal"
android:textColor="#ffff0000"
android:textStyle="bold"
android:textSize="24sp" >
</TextView>
</RelativeLayout>
以下是错误信息:
11-28 01:41:28.612: E/AndroidRuntime(398): FATAL EXCEPTION: main
11-28 01:41:28.612: E/AndroidRuntime(398): java.lang.RuntimeException: Unable to start activity ComponentInfo{org.crazyit.gobang/org.crazyit.gobang.enter}: android.view.InflateException: Binary XML file line #6: Error inflating class org.crazyit.gobang.GobangView
11-28 01:41:28.612: E/AndroidRuntime(398): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
11-28 01:41:28.612: E/AndroidRuntime(398): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
11-28 01:41:28.612: E/AndroidRuntime(398): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
11-28 01:41:28.612: E/AndroidRuntime(398): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
11-28 01:41:28.612: E/AndroidRuntime(398): at android.os.Handler.dispatchMessage(Handler.java:99)
11-28 01:41:28.612: E/AndroidRuntime(398): at android.os.Looper.loop(Looper.java:123)
11-28 01:41:28.612: E/AndroidRuntime(398): at android.app.ActivityThread.main(ActivityThread.java:3683)
11-28 01:41:28.612: E/AndroidRuntime(398): at java.lang.reflect.Method.invokeNative(Native Method)
11-28 01:41:28.612: E/AndroidRuntime(398): at java.lang.reflect.Method.invoke(Method.java:507)
11-28 01:41:28.612: E/AndroidRuntime(398): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
11-28 01:41:28.612: E/AndroidRuntime(398): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
11-28 01:41:28.612: E/AndroidRuntime(398): at dalvik.system.NativeStart.main(Native Method)
11-28 01:41:28.612: E/AndroidRuntime(398): Caused by: android.view.InflateException: Binary XML file line #6: Error inflating class org.crazyit.gobang.GobangView
11-28 01:41:28.612: E/AndroidRuntime(398): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:581)
11-28 01:41:28.612: E/AndroidRuntime(398): at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
11-28 01:41:28.612: E/AndroidRuntime(398): at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
11-28 01:41:28.612: E/AndroidRuntime(398): at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
11-28 01:41:28.612: E/AndroidRuntime(398): at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
11-28 01:41:28.612: E/AndroidRuntime(398): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:207)
11-28 01:41:28.612: E/AndroidRuntime(398): at android.app.Activity.setContentView(Activity.java:1657)
11-28 01:41:28.612: E/AndroidRuntime(398): at org.crazyit.gobang.enter.onCreate(enter.java:30)
11-28 01:41:28.612: E/AndroidRuntime(398): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-28 01:41:28.612: E/AndroidRuntime(398): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
11-28 01:41:28.612: E/AndroidRuntime(398): ... 11 more
11-28 01:41:28.612: E/AndroidRuntime(398): Caused by: java.lang.ClassNotFoundException: org.crazyit.gobang.GobangView in loader dalvik.system.PathClassLoader[/data/app/org.crazyit.gobang-2.apk]
11-28 01:41:28.612: E/AndroidRuntime(398): at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240)
11-28 01:41:28.612: E/AndroidRuntime(398): at java.lang.ClassLoader.loadClass(ClassLoader.java:551)
11-28 01:41:28.612: E/AndroidRuntime(398): at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
11-28 01:41:28.612: E/AndroidRuntime(398): at android.view.LayoutInflater.createView(LayoutInflater.java:471)
11-28 01:41:28.612: E/AndroidRuntime(398): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:570)
11-28 01:41:28.612: E/AndroidRuntime(398): ... 20 more