0

我正在做一个实现 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)
4

3 回答 3

1

看起来mSensorManagernull因为您在尝试在onResume(). onResume()之后立即调用,onCreate()因此您需要在onResume()调用之前对其进行初始化。

我没有使用过这个,所以我绝对不是专家,但在文档中这是他们初始化它的方式

  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);
 }
于 2013-07-29T02:08:15.630 回答
0

尽管您初始化了 mListener,但它的回调函数 onSensorChanged 和 onAccuracyChanged 实际上直到侦听器注册后才被调用。这意味着 mSensorManager 仍然为空。当心。

于 2013-07-29T02:43:34.480 回答
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。祝你好运。

于 2013-07-29T02:54:28.240 回答