ERROR: Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object reference

ERROR: Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object reference

我正在尝试在我的软键盘中实现 MovementDetector,但出现此错误。当我编译我的代码时,一切正常,但是当我开始使用我的键盘时,它在 logcat 中显示错误。这是我的代码:

public class MovementDetector implements SensorEventListener {

protected final String TAG = getClass().getSimpleName();
private Sensor accelerometer;
private SensorManager mSensorMgr;
public static Context mContext;
float [] history = new float[2];
public static String [] direction = {"NONE","NONE"};

private MovementDetector(Context context) {
    mContext = context;
}

private static MovementDetector mInstance;

public static MovementDetector getInstance() {
    if (mInstance == null) {
        mInstance = new MovementDetector(mContext);
        mInstance.init();
    }
    return mInstance;
}

private HashSet<Listener> mListeners = new HashSet<MovementDetector.Listener>();

private void init() {
    mSensorMgr = (SensorManager) mContext.getSystemService(Context.SENSOR_SERVICE);
    accelerometer = mSensorMgr.getDefaultSensor(Sensor.TYPE_LINEAR_ACCELERATION);
}

public void start() {
    mSensorMgr.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_NORMAL);
}

public void stop() {
    mSensorMgr.unregisterListener(this);
}

public void addListener(Listener listener) {
    mListeners.add(listener);
}

@Override
public void onSensorChanged(SensorEvent event) {
    if (event.sensor.getType() == Sensor.TYPE_LINEAR_ACCELERATION) {

        float x = event.values[0];
        float y = event.values[1];
        float z = event.values[2];

        float xChange = history[0] - event.values[0];
        float yChange = history[1] - event.values[1];

        history[0] = event.values[0];
        history[1] = event.values[1];

        float diff = (float) Math.sqrt(x * x + y * y + z * z);
        if (diff > 0.5)
            if (xChange > 2){
                direction[0] = "LEFT";
            }else if (xChange < -2){
                direction[0] = "RIGHT";
            }

            if (yChange > 2){
                direction[1] = "DOWN";
            }else if (yChange < -2){
                direction[1] = "UP";
            }

        for (Listener listener : mListeners) {
            listener.onMotionDetected(event, diff);
        }
    }
}

@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {

}

public interface Listener {
    void onMotionDetected(SensorEvent event, float acceleration);
}

}

这一行出现错误:

mSensorMgr = (SensorManager) mContext.getSystemService(Context.SENSOR_SERVICE);

我在 OnCreateInputView() 中启动 MovementDetector,在 OnDestory() 中停止它,但这不是问题所在。问题出在 MovementDetector class 中。有人可以告诉我我应该做什么以及我做错了什么吗?提前致谢。真的需要这个 MovementDetector 才能工作。

好的,最后根据您在评论中的上述意见,我已经能够确定问题所在。看来 MovementDetector class 使用的是单例设计模式。

要获取 MovementDetector 的实例,您正在使用以下代码

MovementDetector.getInstance()

并且MovementDetector中的getInstance()方法定义为

public static MovementDetector getInstance() {
    if (mInstance == null) {
        mInstance = new MovementDetector(mContext);
        mInstance.init();
    }
    return mInstance;
}

现在,当第一次调用此代码 (getInstance()) 时,mInstance 为空,因此将其实例化。

mInstance = new MovementDetector(mContext);

但上面的问题是 mContext 为空,这就是您收到错误的原因!要解决您的问题,您应该将 context 传递给 getInstance() 方法。它应该看起来像这样。

public static MovementDetector getInstance(Context context) {
        if (mInstance == null) {
            mInstance = new MovementDetector(context);
            mInstance.init();
        }
        return mInstance;
    }

无论你在哪里调用这个方法getInstance()它都应该变成类似

的东西
MovementDetector.getInstance(context).start();