错误:尝试在空对象引用上调用虚方法'java.lang.Object android.content.Context.getSystemService(java.lang.String)'

时间:2015-08-12 01:48:18

标签: android android-context android-sensors ime sensormanager

我正在尝试在我的软键盘中实现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类中。有人可以告诉我我该怎么做以及我做错了什么?提前致谢。真的需要这个MovementDetector才能工作。

1 个答案:

答案 0 :(得分:1)

好的,最后根据您在评论中的上述输入,我已经能够确定问题。似乎MovementDetector类正在使用单例设计模式。

要获取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();