使用加速度计传感器时出现一些错误。这些错误不会影响类的功能,但是当我按下后退按钮时,我的应用程序被“强制关闭”。
09-22 21:33:17.119: E/SensorManager(1397): registerListener :: handle = 0
name= BMA220 delay= 20000 Listener= com.ic.edata.GameActivity@4053b4e8
09-22 21:33:17.119: E/SensorManager(1397): =======>>>Sensor Thread RUNNING <<<========
09-22 21:33:17.129: E/SensorManager(1397): reg :: handle = 0
09-22 21:33:17.139: E/SensorManager(1397): registerListener :: handle = 0
name= BMA220 delay= 20000 Listener= com.ic.edata.GameActivity@4053b4e8
09-22 21:33:17.149: E/SensorManager(1397): reg :: handle = 0
09-22 21:33:36.029: E/SensorManager(1397): unregisterListener:: handle = 0
Listener= com.ic.edata.GameActivity@4053b4e8 name = BMA220
09-22 21:33:36.029: E/SensorManager(1397): unregisterListener:: handle = 0
Listener= com.ic.edata.GameActivity@4053b4e8 name = BMA220
09-22 21:33:36.179: E/SensorManager(1397): unregisterListener:: handle = 0
Listener= com.ic.edata.GameActivity@4053b4e8 name = BMA220
`
我的这个活动的代码是
package com.ic.edata;
import java.util.Random;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
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.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class GameActivity extends Activity implements SensorEventListener {
float x,y,sensorX,sensorY,a,b;
Bitmap ball,end1;
SensorManager sm;
MySurfaceView ourSurfaceView;
Thread ourThread = null;
int count=0;
Sensor s;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sm = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
if(sm.getSensorList(Sensor.TYPE_ACCELEROMETER).size()!=0)
{
s = sm.getSensorList(Sensor.TYPE_ACCELEROMETER).get(0);
sm.registerListener(this, s, SensorManager.SENSOR_DELAY_GAME);
}
ball = BitmapFactory.decodeResource(getResources(), R.drawable.blueball2);
end1 = BitmapFactory.decodeResource(getResources(), R.drawable.end1);
x=y=sensorX=sensorY=0;
a=150;
b=170;
ourSurfaceView = new MySurfaceView(this);
ourSurfaceView.resume();
setContentView(ourSurfaceView);
}
@Override
public void onDestroy() //main thread stopped
{
sm.unregisterListener(this,s);
super.onDestroy();
}
@Override
protected void onPause() {
sm.unregisterListener(this, s);
super.onPause();
}
@Override
protected void onResume() {
sm.registerListener(this, s, SensorManager.SENSOR_DELAY_GAME);
super.onResume();
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub
}
@Override
public void onSensorChanged(SensorEvent event) {
// TODO Auto-generated method stub
try {
Thread.sleep(10);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
sensorX=-event.values[0];
sensorY=event.values[1];
x=x+sensorX;
y=y+sensorY;
}
public class MySurfaceView extends SurfaceView implements Runnable {
SurfaceHolder ourHolder;
boolean isRunning = false;
public MySurfaceView(Context context) {
super(context);
ourHolder = getHolder();
}
public void pause()
{
isRunning=false;
while(true)
{
try {
ourThread.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
}
ourThread=null;
}
public void resume()
{
isRunning=true;
ourThread = new Thread(this);
ourThread.start();
}
@Override
public void run() {
// TODO Auto-generated method stub
while(isRunning)
{
if(!ourHolder.getSurface().isValid())
continue;
Random r = new Random();
Canvas canvas = ourHolder.lockCanvas();
canvas.drawColor(Color.rgb(230, 223, 223));
Paint p1 = new Paint(Color.BLACK);
p1.setTextSize(25);
float endx=a;
float endy=b;
if(x>canvas.getWidth())
x=0-ball.getWidth();
if(y>canvas.getHeight())
y=0-ball.getHeight();
if(x<0-ball.getWidth())
x=canvas.getWidth();
if(y<0-ball.getHeight())
y=canvas.getHeight();
canvas.drawBitmap(end1, endx, endy, null);
if((x>endx-16)&&(x<endx+24)&&(y>endy)&&(y<endy+24))
{
a=r.nextInt(canvas.getWidth()-40);
b=r.nextInt(canvas.getHeight()-40);
x=r.nextInt(canvas.getWidth());
y=r.nextInt(canvas.getHeight());
count++;
}
canvas.drawBitmap(ball, x, y, null);
canvas.drawText("Score : "+count,
canvas.getWidth()/2-45, 20, p1);
ourHolder.unlockCanvasAndPost(canvas);
}
}
}
@Override
public void onBackPressed() {
onDestroy();
super.onBackPressed();
}
}