我正在做一个实现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);
}
}}
这是logcat
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)
答案 0 :(得分:1)
看起来mSensorManager
是null
,因为在尝试在onResume()
中使用它之前,您尚未对其进行初始化。在onResume()
之后立即调用onCreate()
,因此您需要在调用onResume()
之前对其进行初始化。
我还没有使用过这个,所以我绝对不是专家,但在the Docs这就是他们初始化它的方式
public SensorActivity() {
mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
}
protected void onResume() {
super.onResume();
mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL);
}
答案 1 :(得分:0)
虽然你初始化了mListener,但它的回调函数onSensorChanged和onAccuracyChanged在侦听器注册之前实际上还没有被调用过。这意味着mSensorManager仍为空。小心。
答案 2 :(得分:0)
首先,方法registerListener和unregisterListener是不正确的。你需要看到Activity生命周期!所以,你应该将onResume()方法更改为onStart()。 第二,这种方法:
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();
}
,我认为你应该在onDraw方法中执行此操作,在绘制完成之前不应关闭mPath。 祝你好运。