初始化android Sensor Manager时出错

时间:2014-12-27 00:55:14

标签: java android

我一直收到此错误

12-26 13:00:47.564: E/GameScene(21593): Error intializing sensors
12-26 13:00:47.564: E/GameScene(21593): java.lang.NullPointerException
12-26 13:00:47.564: E/GameScene(21593):     at com.magnusworks.justtic_tac_toe.GameScene.create(GameScene.java:45)
12-26 13:00:47.564: E/GameScene(21593):     at com.magnusworks.justtic_tac_toe.SceneManager$1.doInBackground(SceneManager.java:37)
12-26 13:00:47.564: E/GameScene(21593):     at com.magnusworks.justtic_tac_toe.SceneManager$1.doInBackground(SceneManager.java:1)
12-26 13:00:47.564: E/GameScene(21593):     at android.os.AsyncTask$2.call(AsyncTask.java:288)
12-26 13:00:47.564: E/GameScene(21593):     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
12-26 13:00:47.564: E/GameScene(21593):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
12-26 13:00:47.564: E/GameScene(21593):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
12-26 13:00:47.564: E/GameScene(21593):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
12-26 13:00:47.564: E/GameScene(21593):     at java.lang.Thread.run(Thread.java:841)

在此位置

/*44*/try{
        /*45*/sm = (SensorManager) this.activity.getSystemService(Context.SENSOR_SERVICE); //<--- here          
/*46*/  acceleration = sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
            sm.registerListener(this, acceleration, SensorManager.SENSOR_DELAY_NORMAL);
        }
        catch(Exception f){
            Log.e("GameScene", "Error intializing sensors", f);
        }

我是否错误地初始化了传感器管理器?

2 个答案:

答案 0 :(得分:4)

在您的情况下,

this.activitynull

将其替换为:

if (this.activity != null) {
    sm = (SensorManager) this.activity.getSystemService(Context.SENSOR_SERVICE);
}

答案 1 :(得分:1)

你写了一个易受攻击的声明:

$subject = $_POST['mounth'];

将此语句修改为:

def rshift(val, n):
    s = val & 0x80000000
    for i in range(0,n):
        val >>= 1
        val |= s
    return val

当我们在mainActivity.java类中扩展Activity类时,方法sm = (SensorManager) this.activity.getSystemService(Context.SENSOR_SERVICE); 可供您使用。不需要写sm = (SensorManager) getSystemService(Context.SENSOR_SERVICE);