Google Androidの傾きセンサーの値によって絵を動かす

いままでTextViewで文字列を表示させていたが、絵を表示させるコードを追加したらNullPointerExceptionで起動しなくなって困った。どう直せばいいかわからなかったので、CanvasのdrawTextメソッドで文字列を表示するようにした。
やはり、Java言語の書き方がよくわからない。たくさん書いて、読んでを続けないと掴めなさそう。
あと、方位センサーの座標系をまだ理解できていない。
わからないなりに、プログラムは作ることはできた。次は加速度を考慮するようにしたいな。

package com.example.android.movebitmap;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.View;

public class MoveBitmap extends Activity implements SensorEventListener {
	private Sensor orientationSensor;
	private Sensor accelerometerSensor;
	private float[] orientationValues = new float[3];
	private float[] accelerometerValues = new float[3];
	private MoveBitmapView moveBitmapView;
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
//      setContentView(R.layout.main);

		moveBitmapView = new MoveBitmapView(this);
		setContentView(moveBitmapView);

		SensorManager sm = (SensorManager)getSystemService(SENSOR_SERVICE);

		List<Sensor> sensorList;
		sensorList = sm.getSensorList(Sensor.TYPE_ORIENTATION);
		orientationSensor = sensorList.get(0);
		sensorList = sm.getSensorList(Sensor.TYPE_ACCELEROMETER);
		accelerometerSensor = sensorList.get(0);

		sm.registerListener(this, orientationSensor, SensorManager.SENSOR_DELAY_NORMAL);
		sm.registerListener(this, accelerometerSensor, SensorManager.SENSOR_DELAY_NORMAL);
	}

	public void onSensorChanged(SensorEvent event) {
		if (event.sensor == orientationSensor) {
			orientationValues = event.values;
			moveBitmapView.setOrientationText(orientationValues);
			moveBitmapView.move(orientationValues);
	        moveBitmapView.invalidate();
		}
		else if (event.sensor == accelerometerSensor) {
			accelerometerValues = event.values;
			moveBitmapView.setAccelerometerText(accelerometerValues);
	        moveBitmapView.invalidate();
		}
	}

	public void onAccuracyChanged(Sensor sensor, int accuracy) {
	}
}

class MoveBitmapView extends View {
	private Bitmap bitmap;
	private static int x, y;
	private String[] orientationString = new String[3];
	private String[] accelerometerString = new String[3];
	
	public MoveBitmapView(Context context) {
		super(context);

		Resources r = getResources();
		bitmap = BitmapFactory.decodeResource(r, R.drawable.mini);
		x = 100;
		y = 100;

		orientationString[0] = "Azimuth:";
		orientationString[1] = "Pitch  :";
		orientationString[2] = "Roll   :";
        
		accelerometerString[0] = "X:";
		accelerometerString[1] = "Y:";
		accelerometerString[2] = "Z:";
	}
	
	@Override
	public void onDraw(Canvas canvas) {
		canvas.drawBitmap(bitmap, x, y, null);

		final int SENSOR_TEXT_SIZE = 16;
		Paint paint = new Paint();
		paint.setAntiAlias(true);
		paint.setTextSize(SENSOR_TEXT_SIZE);
		paint.setColor(0xFFFFFFFF);
		canvas.drawText("SENSOR_ORIENTATION:", 0, SENSOR_TEXT_SIZE, paint);
		for (int i=0; i<orientationString.length; i++) {
			canvas.drawText(orientationString[i], 20, SENSOR_TEXT_SIZE+SENSOR_TEXT_SIZE*(i+1), paint);
		}
		canvas.drawText("SENSOR_ACCELEROMETER:", 0, 80, paint);
		for (int i=0; i<accelerometerString.length; i++) {
			canvas.drawText(accelerometerString[i], 20, 80+SENSOR_TEXT_SIZE*(i+1), paint);
		}
	}
	
	public void move(float[] orientationValues) {
		final int DELTA = 3;
		final int ALLOWANCE = 10;
		
		if (orientationValues[2] < ALLOWANCE*-1) {
			this.moveX(DELTA);
		}
		else if (orientationValues[2] > ALLOWANCE) {
			this.moveX(DELTA*-1);
		}

		if (orientationValues[1] < ALLOWANCE*-1) {
			this.moveY(DELTA);
		}
		else if (orientationValues[1] > ALLOWANCE) {
	        this.moveY(DELTA*-1);
		}
	}
	
	private void moveX(int dx) {
		x += dx;
	}
	
	private void moveY(int dy) {
		y += dy;
	}
	
	public void setOrientationText(float[] values) {
		orientationString[0] = "Azimuth:"+(int)values[0];
		orientationString[1] = "Pitch  :"+(int)values[1];
		orientationString[2] = "Roll   :"+(int)values[2];
	}
	
	public void setAccelerometerText(float[] values) {
		accelerometerString[0] = "X:"+(int)values[0];
		accelerometerString[1] = "Y:"+(int)values[1];
		accelerometerString[2] = "Z:"+(int)values[2];
	}
}