Merge pull request #79681 from m4gr3d/fix_npe_main

Fix NullPointerException when registering the sensors
This commit is contained in:
Yuri Sizov 2023-07-31 21:01:25 +02:00
commit 9fbb349a33

View file

@ -88,16 +88,16 @@ class Godot(private val context: Context) : SensorEventListener {
private val mSensorManager: SensorManager by lazy { private val mSensorManager: SensorManager by lazy {
requireActivity().getSystemService(Context.SENSOR_SERVICE) as SensorManager requireActivity().getSystemService(Context.SENSOR_SERVICE) as SensorManager
} }
private val mAccelerometer: Sensor by lazy { private val mAccelerometer: Sensor? by lazy {
mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER) mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER)
} }
private val mGravity: Sensor by lazy { private val mGravity: Sensor? by lazy {
mSensorManager.getDefaultSensor(Sensor.TYPE_GRAVITY) mSensorManager.getDefaultSensor(Sensor.TYPE_GRAVITY)
} }
private val mMagnetometer: Sensor by lazy { private val mMagnetometer: Sensor? by lazy {
mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD) mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD)
} }
private val mGyroscope: Sensor by lazy { private val mGyroscope: Sensor? by lazy {
mSensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE) mSensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE)
} }
private val mClipboard: ClipboardManager by lazy { private val mClipboard: ClipboardManager by lazy {
@ -492,10 +492,18 @@ class Godot(private val context: Context) : SensorEventListener {
} }
renderView!!.onActivityResumed() renderView!!.onActivityResumed()
if (mAccelerometer != null) {
mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_GAME) mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_GAME)
}
if (mGravity != null) {
mSensorManager.registerListener(this, mGravity, SensorManager.SENSOR_DELAY_GAME) mSensorManager.registerListener(this, mGravity, SensorManager.SENSOR_DELAY_GAME)
}
if (mMagnetometer != null) {
mSensorManager.registerListener(this, mMagnetometer, SensorManager.SENSOR_DELAY_GAME) mSensorManager.registerListener(this, mMagnetometer, SensorManager.SENSOR_DELAY_GAME)
}
if (mGyroscope != null) {
mSensorManager.registerListener(this, mGyroscope, SensorManager.SENSOR_DELAY_GAME) mSensorManager.registerListener(this, mGyroscope, SensorManager.SENSOR_DELAY_GAME)
}
if (useImmersive) { if (useImmersive) {
val window = requireActivity().window val window = requireActivity().window
window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_STABLE or window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_STABLE or