Reputation: 23
I'm trying to develop a small android app. You are controling your character, which is astronaut, and you are supposed to gather air bubbles to survive. Well, I got stucked at the very beginning. I want the character to be controled by accelerometer, but I can't solve the errors I get.
My accelerometer is implemented in activity Game, which worked before adding accelerometer. Game has as parameter in setContentView() instance of class GameView, which sets my GameRenderer.
I only need the x coordinates and I need to work with them in my GameRenderer, specifically in movePlayer(). So, I added instance of Game to my Engine class, where I keep all this stuff, and in movePlayer() I use Engine.game.x.
There's the problem. When I try to run it, I get NullPointerException, and log says errors are on lines 51 and 83.
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.app.Activity;
public class Game extends Activity implements SensorEventListener {
private GameView gameView;
SensorManager mSensorManager;
Sensor mAccelerometer;
public float x = 0;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
gameView = new GameView(this);
setContentView(gameView);
mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL);
}
@Override
protected void onResume(){
super.onResume();
gameView.onResume();
}
@Override
protected void onPause(){
super.onPause();
gameView.onPause();
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub
}
@Override
public void onSensorChanged(SensorEvent event) {
x = event.values[0];
}
}
Here's my movePlayer method:
private void movePlayer(GL10 gl) {
lastX = Engine.game.x; //**line 83**
if (Engine.game.x * 10 > lastX) {
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();
gl.glPushMatrix();
gl.glScalef(.25f, .25f, .25f);
gl.glTranslatef(Engine.game.x, 0f, 0f);
gl.glMatrixMode(GL10.GL_TEXTURE);
gl.glLoadIdentity();
gl.glTranslatef(0.25f, 0.0f, 0.0f);
player.draw(gl);
gl.glPopMatrix();
gl.glLoadIdentity();
}
else if (Engine.game.x * 10 < lastX) {
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();
gl.glPushMatrix();
gl.glScalef(.25f, .25f, 1f);
gl.glTranslatef(Engine.game.x, 0f, 0f);
gl.glMatrixMode(GL10.GL_TEXTURE);
gl.glLoadIdentity();
gl.glTranslatef(0.75f, 0.0f, 0.0f);
player.draw(gl);
gl.glPopMatrix();
gl.glLoadIdentity();
}
else { //postava se nehýbe
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();
gl.glPushMatrix();
gl.glScalef(.25f, .25f, 1f);
gl.glTranslatef(Engine.game.x, 0f, 0f);
gl.glMatrixMode(GL10.GL_TEXTURE);
gl.glLoadIdentity();
gl.glTranslatef(0.0f, 0.0f, 0.0f);
player.draw(gl);
gl.glPopMatrix();
gl.glLoadIdentity();
}
}
And here's the Game instance in my Engine:
public static Game game;
Line 51 is simply calling movePlayer(). I'm trying to be very specific, so you guys have all the details and hopefully you could help me. Thanks for any advice, I'm getting really desperate.
Upvotes: 2
Views: 148
Reputation: 13177
I would expect Engine.game
is null
. It's hard to see how your code works exactly, but if I understand correctly, you should set Engine.game
in the onCreate
of your game:
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
Engine.game = this; // <-- add this
gameView = new GameView(this);
setContentView(gameView);
mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL);
}
Upvotes: 1