我开始为 android 制作一个游戏项目,我制作了 2 个活动(mainactivity 和 Pvsp),然后是我之前描述的所有活动,我在清单中添加了。但是logcat给我这样的错误
11-08 15:50:10.339: E/Trace(6221): error opening trace file: No such file or directory (2)
11-08 15:50:10.339: V/ActivityThread(6221): com.rhas.skripsiletsgo white listed for hwui
11-08 15:50:14.339: D/dalvikvm(6221): newInstance failed: no <init>()
11-08 15:50:14.349: D/AndroidRuntime(6221): Shutting down VM
11-08 15:50:14.359: W/dalvikvm(6221): threadid=1: thread exiting with uncaught exception (group=0x40a8e930)
11-08 15:50:14.389: E/AndroidRuntime(6221): FATAL EXCEPTION: main
11-08 15:50:14.389: E/AndroidRuntime(6221): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.rhas.skripsiletsgo/com.rhas.skripsiletsgo.Pvsp}: java.lang.InstantiationException: can't instantiate class com.rhas.skripsiletsgo.Pvsp; no empty constructor
11-08 15:50:14.389: E/AndroidRuntime(6221): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2223)
11-08 15:50:14.389: E/AndroidRuntime(6221): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2357)
11-08 15:50:14.389: E/AndroidRuntime(6221): at android.app.ActivityThread.access$600(ActivityThread.java:153)
11-08 15:50:14.389: E/AndroidRuntime(6221): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1247)
11-08 15:50:14.389: E/AndroidRuntime(6221): at android.os.Handler.dispatchMessage(Handler.java:99)
11-08 15:50:14.389: E/AndroidRuntime(6221): at android.os.Looper.loop(Looper.java:137)
11-08 15:50:14.389: E/AndroidRuntime(6221): at android.app.ActivityThread.main(ActivityThread.java:5259)
11-08 15:50:14.389: E/AndroidRuntime(6221): at java.lang.reflect.Method.invokeNative(Native Method)
11-08 15:50:14.389: E/AndroidRuntime(6221): at java.lang.reflect.Method.invoke(Method.java:511)
11-08 15:50:14.389: E/AndroidRuntime(6221): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
11-08 15:50:14.389: E/AndroidRuntime(6221): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
11-08 15:50:14.389: E/AndroidRuntime(6221): at dalvik.system.NativeStart.main(Native Method)
11-08 15:50:14.389: E/AndroidRuntime(6221): Caused by: java.lang.InstantiationException: can't instantiate class com.rhas.skripsiletsgo.Pvsp; no empty constructor
11-08 15:50:14.389: E/AndroidRuntime(6221): at java.lang.Class.newInstanceImpl(Native Method)
11-08 15:50:14.389: E/AndroidRuntime(6221): at java.lang.Class.newInstance(Class.java:1319)
11-08 15:50:14.389: E/AndroidRuntime(6221): at android.app.Instrumentation.newActivity(Instrumentation.java:1054)
11-08 15:50:14.389: E/AndroidRuntime(6221): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2214)
11-08 15:50:14.389: E/AndroidRuntime(6221): ... 11 more
11-08 15:50:16.099: I/Process(6221): Sending signal. PID: 6221 SIG: 9
那么这是我的清单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.rhas.skripsiletsgo"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="10" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.rhas.skripsiletsgo.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>
<activity
android:name="com.rhas.skripsiletsgo.Pvsp"
android:label="@string/app_name">
</activity>
</application>
</manifest>
这是我的项目,请随时进行编辑、分析等。抱歉我的英语不好。感谢所有帮助/评论。
编辑:这是 Pvsp 类
package com.rhas.skripsiletsgo;
import static com.rhas.skripsiletsgo.core.StoneColor.BLACK;
import java.util.HashSet;
import java.util.Set;
import java.util.Timer;
import java.util.TimerTask;
import com.rhas.skripsiletsgo.StoneDrawable;
import com.rhas.skripsiletsgo.core.KoRuleException;
import com.rhas.skripsiletsgo.core.NoLibertiesException;
import com.rhas.skripsiletsgo.core.SpaceTakenException;
import com.rhas.skripsiletsgo.core.Game;
import com.rhas.skripsiletsgo.core.GameState;
import com.rhas.skripsiletsgo.core.Stone;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.ViewGroup;
import android.widget.Toast;
public class Pvsp extends ViewGroup{
public Pvsp(Context context, AttributeSet attrs) {
super(context, attrs);
setKeepScreenOn(true);
stones = new Stone[game.getTableSize()][game.getTableSize()];
blackStoneDrawable = new StoneDrawable.Black();
whiteStoneDrawable = new StoneDrawable.White();
gridDrawable = new GridDrawable(game.getTableSize());
setGameState(game.getLastState());
game.addListener(new GameListener());
}
static final int MAX_DRAWN_CAPTURED_STONES = 16;
final static int PADDING = 3;
final GridDrawable gridDrawable;
final Rect rect = new Rect();
final Stone[][] stones;
StoneDrawable blackStoneDrawable;
StoneDrawable whiteStoneDrawable;
long lastTouchMove = -1;
Game game;
GameState gameState;
int cellSize;
private boolean skipNextTouchUp;
void setGameState(GameState state){
if(gameState != null){
Set<Stone> prevStones = gameState.getStones();
Set<Stone> newStones = state.getStones();
Set<Stone> prevStonesM = new HashSet<Stone>(prevStones);
Set<Stone> newStonesM = new HashSet<Stone>(newStones);
prevStonesM.removeAll(newStones);
for(Stone stone : prevStonesM){
stones[stone.getRow()][stone.getCol()] = null;
}
newStonesM.removeAll(prevStones);
for(Stone stone : newStonesM){
stones[stone.getRow()][stone.getCol()] = stone;
}
invalidate();
}
gameState = state;
}
private int getX(int row){
int l = rect.left + cellSize / 2 + PADDING;
int r = rect.right - cellSize / 2 - PADDING;
return getCoord(row, l, r);
}
private int getY(int col){
int t = rect.top + cellSize / 2 + PADDING;
int b = rect.bottom + cellSize / 2 - PADDING;
return getCoord(col, t, b);
}
private int getCoord(int i, int min, int max){
return min + (max - min) * i / (game.getTableSize() - 1);
}
private int getRow(float y){
return Math.round(0.5f + (y - PADDING) / (rect.bottom - rect.top - 2*PADDING) * game.getTableSize() - 1);
}
private int getCol(float x){
return Math.round(0.5f + (x - PADDING) / (rect.right - rect.left - 2*PADDING) * game.getTableSize() - 1);
}
protected void onDraw(Canvas canvas){
super.onDraw(canvas);
canvas.save();
int p = PADDING + cellSize / 2;
canvas.clipRect(p, p, rect.width() - p, rect.height() - p);
gridDrawable.draw(canvas);
canvas.restore();
drawStones(canvas);
drawCapturedStones(canvas);
drawWhoseTurn(canvas);
}
private void drawStones(Canvas canvas) {
// TODO Auto-generated method stub
for(int i = 0; i < stones.length; i++){
for(int j = 0; j < stones[i].length; j++){
Stone stone = stones[i][j];
if(stone == null) continue;
int x = getX(stone.getCol());
int y = getY(stone.getRow());
canvas.save();
canvas.translate(x - cellSize / 2, y - cellSize / 2);
if(stone.getColor() == BLACK){
blackStoneDrawable.draw(canvas);
}else{
whiteStoneDrawable.draw(canvas);
}
canvas.restore();
}
}
}
private void drawCapturedStones(Canvas canvas) {
// TODO Auto-generated method stub
int dx = PADDING + cellSize / 2;
int dy = rect.height() + cellSize;
canvas.save();
canvas.translate(dx, dy);
drawCapturedStones(canvas, gameState.getBlacksCaptured(), blackStoneDrawable, false);
canvas.restore();
canvas.save();
canvas.translate(canvas.getWidth() - dx - cellSize, dy);
drawCapturedStones(canvas, gameState.getWhitesCaptured(), whiteStoneDrawable, true);
canvas.restore();
}
private void drawCapturedStones(Canvas canvas, int count, StoneDrawable drawable, boolean flip){
if(count > MAX_DRAWN_CAPTURED_STONES){
count = MAX_DRAWN_CAPTURED_STONES;
}
for(int i = 0; i < count; i++){
int rowCol = Util.getCapturedRowCol(i);
int row = (rowCol / 1000);
int col = rowCol % 1000;
if(flip){
col = -col;
}
canvas.save();
int x = col * cellSize;
int y = row * cellSize;
canvas.translate(x, y);
drawable.draw(canvas);
canvas.restore();
}
}
private void drawWhoseTurn(Canvas canvas) {
// TODO Auto-generated method stub
int dx = 5 * cellSize + cellSize / 2 + PADDING;
int dy = rect.height();
canvas.save();
StoneDrawable drawable;
if (gameState.getWhoseTurn() == BLACK) {
// Flip horizontally.
canvas.translate(rect.width() - dx - 2 * cellSize, dy);
drawable = blackStoneDrawable;
} else {
canvas.translate(dx, dy);
drawable = whiteStoneDrawable;
}
int oldSize = drawable.getSize();
drawable.setSize(2 * cellSize);
drawable.draw(canvas);
drawable.setSize(oldSize);
canvas.restore();
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
// TODO Auto-generated method stub
if (!changed) return;
rect.left = l;
rect.top = t;
rect.right = Math.min(r, b);
rect.bottom = Math.min(r, b);
cellSize = (rect.width() - 2 * PADDING) / game.getTableSize();
blackStoneDrawable.setSize(cellSize);
whiteStoneDrawable.setSize(cellSize);
}
void skipNextTouchUp() {
this.skipNextTouchUp = true;
}
class PlaceStoneTimer extends Timer {
}
PlaceStoneTimer placeStoneTimer = new PlaceStoneTimer();
class PlaceStoneTimerTask extends TimerTask {
@Override
public void run() {
gridDrawable.highlight();
postInvalidate();
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
int row = getRow(event.getY());
int col = getCol(event.getX());
gridDrawable.unhighlight();
invalidate();
if (row < 0 || row >= game.getTableSize() || col < 0 || col >= game.getTableSize()) {
return true;
}
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_MOVE:
if (gridDrawable.getHighlightedRow() == null) {
gridDrawable.prehighlight(row, col);
placeStoneTimer.schedule(new PlaceStoneTimerTask(), 50000);
} else if (gridDrawable.getHighlightedRow() != row
|| gridDrawable.getHighlightedCol() != col) {
gridDrawable.prehighlight(row, col);
placeStoneTimer.cancel();
placeStoneTimer.schedule(new PlaceStoneTimerTask(), 50000);
}
return true;
case MotionEvent.ACTION_UP:
if (skipNextTouchUp) {
skipNextTouchUp = false;
return true;
}
try {
game.makeTurnAt(row, col);
} catch (SpaceTakenException e) {
// Toast.makeText(getContext(), "This space is taken",
// Toast.LENGTH_SHORT).show();
} catch (NoLibertiesException e) {
Toast.makeText(getContext(), "No liberty", Toast.LENGTH_SHORT).show();
} catch (KoRuleException e) {
String msg = "Ko rule violation";
if (e.getTurnsAgo() > 2) {
msg = "Super ko rule violation " + e.getTurnsAgo() + " turns ago";
}
Toast.makeText(getContext(), msg, Toast.LENGTH_SHORT).show();
}
}
return true;
}
private class GameListener implements com.rhas.skripsiletsgo.core.GameListener {
public void onStateAdvanced(GameState newState) {
setGameState(newState);
}
public void onGameReset() {
for (int row = 0; row < game.getTableSize(); row++) {
for (int col = 0; col < game.getTableSize(); col++) {
stones[row][col] = null;
}
}
removeAllViews();
}
}
}