gpt4 book ai didi

java - 初始化 android 传感器管理器时出错

转载 作者:太空狗 更新时间:2023-10-29 16:36:28 25 4
gpt4 key购买 nike

我一直收到这个错误

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);
}

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

最佳答案

this.activity 在您的情况下为 null

替换为:

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

关于java - 初始化 android 传感器管理器时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27663457/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com