-1

My game crashes whenever I try to open it. It gives me this message in my GameView: Custom view GameView is missing constructor used by tools: (Context) or (Context,AttributeSet) or (Context,AttributeSet,int)

I tried all three different constructors, I've tried a few constructors at the same time and even though the message sometimes goes away my app still crashes when I try to open it. I've been at it for three days, searched all over but could not find an answer.

Here's my MainActivity.java code:

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();
    }
}

Here's my GameView.java code:

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 code:

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;
}
}
mpromonet
  • 11,326
  • 43
  • 62
  • 91
nzn
  • 11
  • 5
  • 2
    Possibly related: http://stackoverflow.com/questions/17063090/custom-view-is-missing-constructor-used-by-tools-for-adapter – Edd Aug 28 '15 at 18:44
  • I saw that and tried it, but it didn't work. – nzn Aug 28 '15 at 18:54
  • "I've tried a few constructors at the same time and even though the message sometimes goes away my app still crashes when I try to open it." Well then, it sounds like you've fixed the constructor issue and your application has multiple issues. Post multiple questions and/or include exception messages with full stack traces. – DavidS Aug 28 '15 at 19:00

1 Answers1

0

I have a hunch that the problem could be caused by the x and y in your constructor. Just to experiment, try dropping the x and y and just set those values as constants, then implement all three default view constructors.

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);
}
...
Alex Crist
  • 1,059
  • 2
  • 12
  • 22