我正在做一个实现 SensorEventListener 的应用程序,但我的 onResume 方法有问题 logcat 告诉我有一个 nullpointerexception 但我不明白为什么这是我的活动
public class T11Ej2 extends Activity {
private SensorManager mSensorManager;
private Sensor mSensor;
private SampleView mView;
private float[] mValues;
private final SensorEventListener mListener = new SensorEventListener() {
@Override
public void onSensorChanged(SensorEvent event) {
// TODO Auto-generated method stub
mValues = event.values;
if(mView != null){
mView.invalidate();
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
mSensorManager.registerListener(mListener, mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD), SensorManager.SENSOR_DELAY_NORMAL);
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mView = new SampleView(this);
setContentView(mView);
}
@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
mSensorManager.unregisterListener(mListener);
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
mSensorManager.registerListener(mListener,
mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
SensorManager.SENSOR_DELAY_NORMAL);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.t11_ej2, menu);
return true;
}
// Código para pintar la brujula
private class SampleView extends View{
private Paint mPaint = new Paint();
private Path mPath = new Path();
public SampleView(Context context) {
super(context);
// TODO Auto-generated constructor stub
mPath.moveTo(0, -50);
mPath.lineTo(-20, 60);
mPath.lineTo(0, 50);
mPath.lineTo(20, 60);
mPath.close();
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
Paint paint = mPaint;
canvas.drawColor(Color.WHITE);
paint.setAntiAlias(true);
paint.setColor(Color.BLACK);
paint.setStyle(Paint.Style.FILL);
int w = canvas.getWidth();
int h = canvas.getHeight();
int cx = w / 2;
int cy = h / 2;
canvas.translate(cx, cy);
if (mValues != null) {
canvas.rotate(-mValues[0]);
}
canvas.drawPath(mPath, mPaint);
}
}}
这是日志猫
07-29 01:51:27.758: E/AndroidRuntime(3236): FATAL EXCEPTION: main
07-29 01:51:27.758: E/AndroidRuntime(3236): java.lang.RuntimeException: Unable to resume activity {com.example.t11ej2/com.example.t11ej2.T11Ej2}: java.lang.NullPointerException
07-29 01:51:27.758: E/AndroidRuntime(3236): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2742)
07-29 01:51:27.758: E/AndroidRuntime(3236): at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2771)
07-29 01:51:27.758: E/AndroidRuntime(3236): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2235)
07-29 01:51:27.758: E/AndroidRuntime(3236): at android.app.ActivityThread.access$600(ActivityThread.java:141)
07-29 01:51:27.758: E/AndroidRuntime(3236): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
07-29 01:51:27.758: E/AndroidRuntime(3236): at android.os.Handler.dispatchMessage(Handler.java:99)
07-29 01:51:27.758: E/AndroidRuntime(3236): at android.os.Looper.loop(Looper.java:137)
07-29 01:51:27.758: E/AndroidRuntime(3236): at android.app.ActivityThread.main(ActivityThread.java:5041)