每当我尝试打开它时,我的游戏都会崩溃。它在我的GameView中给我这个消息:自定义视图GameView缺少工具使用的构造函数:(上下文)或(上下文,AttributeSet)或(上下文,AttributeSet,int)
我尝试了所有三种不同的构造函数,我同时尝试了一些构造函数,即使消息有时会消失,但当我尝试打开它时,我的应用程序仍会崩溃。我已经在这里待了三天,全身都搜索过但找不到答案。
这是我的MainActivity.java代码:
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends ActionBarActivity implements View.OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button buttonPlay =(Button)findViewById(R.id.buttonPlay);
buttonPlay.setOnClickListener(this);
}
@Override
public void onClick(View v) {
Intent i = new Intent(this, GameActivity.class);
startActivity(i);
finish();
}
}
这是我的GameView.java代码:
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class GameView extends SurfaceView implements Runnable{
volatile boolean playing;
Thread gameThread = null;
//Game objects
private PlayerShip player;
// For drawing
private Paint paint;
private Canvas canvas;
private SurfaceHolder ourHolder;
GameView(Context context, int x, int y) {
super(context);
ourHolder = getHolder();
paint = new Paint();
player = new PlayerShip(context, x, y );
}
@Override
public void run() {
while (playing) {
update();
draw();
control();
}
}
private void update(){
}
private void draw(){
player.update();
if (ourHolder.getSurface().isValid()) {
canvas = ourHolder.lockCanvas();
canvas.drawColor(Color.argb(255, 0, 0, 0));
canvas.drawBitmap(
player.getBitmap(),
player.getX(),
player.getY(),
paint );
ourHolder.unlockCanvasAndPost(canvas);
}
}
private void control(){
try {
gameThread.sleep(17);
} catch (InterruptedException e) {
}
}
public void pause() {
playing = false;
try {
gameThread.join();
} catch (InterruptedException e) {
}
}
public void resume() {
playing = true;
gameThread = new Thread(this);
gameThread.start();
}
@Override
public boolean onTouchEvent(MotionEvent motionEvent) {
switch (motionEvent.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_UP:
player.stopBoosting();
break;
case MotionEvent.ACTION_DOWN:
player.setBoosting();
break;
}
return true;
}
}
GameActivity.java:
import android.graphics.Point;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.ActionBarActivity;
import android.view.Display;
public class GameActivity extends ActionBarActivity {
private GameView gameView;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get a Display object to access screen details
Display display = getWindowManager().getDefaultDisplay();
// Load the resolution into a Point object
Point size = new Point();
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2){
display.getSize(size);
}else{
size.x = display.getWidth();
size.y = display.getHeight();
}
//overrideGetSize(display, size);
gameView = new GameView(this, size.x, size.y);
setContentView(gameView);
}
@Override
protected void onPause() {
super.onPause();
gameView.pause();
}
@Override
protected void onResume() {
super.onResume();
gameView.resume();
}
}
PlayerShip.java代码:
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
public class PlayerShip {
private final int GRAVITY = -12;
// Stop ship leaving the screen
private int maxY;
private int minY;
//Limit the bounds of the ship's speed
private final int MIN_SPEED = 1;
private final int MAX_SPEED = 20;
private boolean boosting;
private Bitmap bitmap;
private int x, y;
private int speed = 0;
public PlayerShip(Context context, int screenX, int screenY) {
maxY = screenY - bitmap.getHeight();
minY = 0;
boosting = false;
bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.ship);
this.x = 50;
this.y = 50;
speed = 1;
}
public void update() {
if (boosting) {
speed += 2;
} else {
speed -= 5;
}
// Constrain top speed
if (speed > MAX_SPEED) {
speed = MAX_SPEED;
}
// Never stop completely
if (speed < MIN_SPEED) {
speed = MIN_SPEED;
}
// move the ship up or down
y -= speed + GRAVITY;
// But don't let ship stray off screen
if (y < minY) {
y = minY;
}
if (y > maxY) { y = maxY;
}
}
//Getters
public Bitmap getBitmap() {
return bitmap;
}
public int getSpeed() {
return speed;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public void setBoosting() { boosting = true;
}
public void stopBoosting() { boosting = false;
}
}
答案 0 :(得分:0)
我预感到问题可能是由构造函数中的x
和y
引起的。只是为了试验,尝试删除public class GameView extends SurfaceView implements Runnable{
...
GameView(Context context) {
super(context);
init(context);
}
GameView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
GameView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context);
}
private void init(Context context) {
int x = 500;
int y = 500;
ourHolder = getHolder();
paint = new Paint();
player = new PlayerShip(context, x, y);
}
...
和imageView = (ImageView) checkableButton.getChildAt(position);
并将这些值设置为常量,然后实现所有三个默认视图构造函数。
weightsum