So I am making an android breakout game to get situated with java and the android API. I have very similar code to what they show on this webpage (SensorManager | Android Developers), but my code crashes. I do implement the class over in another file which is also attached. Can anyone point me in the direction as to why this is crashing? Is there some command I need to call first before I get a system service?
package com.example.breakout; import android.app.Activity; import android.hardware.Sensor; import android.hardware.SensorEvent; import android.hardware.SensorEventListener; import android.hardware.SensorManager; public class UpdateAccel extends Activity implements SensorEventListener { private boolean ENABLED = false; private Sensor accel; private SensorManager sm; float aX = 0,aY = 0,aZ = 0; public UpdateAccel() { sm = (SensorManager) getSystemService(SENSOR_SERVICE); //This line here crashes if I comment it out all runs well. /*if(sm.getSensorList(Sensor.TYPE_ACCELEROMETER).size() == 0) { ENABLED = false; //no accelerometer installed! }else { ENABLED = true; //accelerometer is installed! accel = sm.getSensorList(Sensor.TYPE_ACCELEROMETER).get(0); if(!sm.registerListener(this, accel,SensorManager.SENSOR_DELAY_GAME)) { ENABLED = false; //something went wrong during registering. } }*/ } public float getXacc() { if(!ENABLED)return 0; return aX; } public float getYacc() { if(!ENABLED)return 0; return aY; } public float getZacc() { if(!ENABLED)return 0; return aZ; } @Override public void onAccuracyChanged(Sensor arg0, int arg1) { // TODO Auto-generated method stub } @Override public void onSensorChanged(SensorEvent arg0) { if(ENABLED) { aX = (float)(arg0.values[0]/9.8); aY = (float)(arg0.values[1]/9.8); aZ = (float)(arg0.values[2]/9.8); if(aX < -1) aX = -1; if(aY < -1) aY = -1; if(aZ < -1) aZ = -1; if(aX > 1) aX = 1; if(aY > 1) aY = 1; if(aZ > 1) aZ = 1; } } }
package com.example.breakout; import android.content.Context; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Color; import android.view.SurfaceHolder; import android.view.SurfaceView; public class GameView extends SurfaceView implements SurfaceHolder.Callback { private SpriteObject[] Bricks = new SpriteObject[10]; private SpriteObject Ball; private SpriteObject Paddle; private GameLogic mGameLogic; UpdateAccel myAccel; public GameView(Context context) { super(context); getHolder().addCallback(this); myAccel = new UpdateAccel(); for(int i = 0; i < 10; i++) { Bricks[i] = new SpriteObject(BitmapFactory.decodeResource(getResources(), R.drawable.redbrick), 10+i*20, 50); } Ball = new SpriteObject(BitmapFactory.decodeResource(getResources(), R.drawable.ball), 100, 100); Ball.setMoveY(1); Ball.setMoveX(1); Paddle = new SpriteObject(BitmapFactory.decodeResource(getResources(), R.drawable.paddle), getWidth()/2, getHeight()/2); mGameLogic = new GameLogic(getHolder(), this); setFocusable(true); } @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { } @SuppressWarnings("static-access") @Override public void surfaceCreated(SurfaceHolder holder) { mGameLogic.setGameState(mGameLogic.RUNNING); mGameLogic.start(); } @Override public void surfaceDestroyed(SurfaceHolder holder) { } public void onDraw(Canvas canvas) { canvas.drawColor(Color.BLACK); for(int i = 0;i < 10;i++) { if(Bricks[i] != null) { Bricks[i].draw(canvas); } } Paddle.draw(canvas); Ball.draw(canvas); } public void update(int adj_mov) { //handles when ball hits side of the screen if(Ball.getX() <= 5 && Ball.getMoveX() < 0)Ball.setMoveX(Ball.getMoveX() * -1); if(Ball.getX() >= getWidth() - 5 && Ball.getMoveX() > 0)Ball.setMoveX(Ball.getMoveX() * -1); if(Ball.getY() <= 5 && Ball.getMoveY() < 0)Ball.setMoveY(Ball.getMoveY() * -1); //done with screen //for testing bounces. if(Ball.getY() >= getHeight() - 5 && Ball.getMoveY() > 0)Ball.setMoveY(Ball.getMoveY()*-1); //end testing Ball.update(adj_mov); Paddle.setX((int)(getWidth()/2 + myAccel.getXacc()*(getWidth()/2))); Paddle.setY(getHeight()-15); } }