I got the following problem in Android Studio. I tried to rebuild an Google Android Guide for using the light sensor. While typing the code I got this error from Android Studio. It says Unresolved reference: event I don't know how to handle it. It is one of my first tries using kotlin in Android Studio.
Can anyone show me how to fix it?
I am using Android Studio Version 2021.3.1 Patch 1 on Mac OS X Catalina
I am thankful for any help.
Here is my Code:
package eu.jnaberle.myapplication
import android.app.Activity
import android.hardware.Sensor
import android.hardware.SensorEvent
import android.hardware.SensorEventListener
import android.hardware.SensorManager
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
}
class SensorActivity : Activity(), SensorEventListener {
private lateinit var sensorManager: SensorManager
private var mLight: Sensor? = null
public override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
sensorManager = getSystemService(SENSOR_SERVICE) as SensorManager
mLight = sensorManager.getDefaultSensor(Sensor.TYPE_LIGHT)
}
override fun onAccuracyChanged(p0: Sensor?, p1: Int) {
TODO("Not yet implemented")
}
override fun onSensorChanged(p0: SensorEvent?) {
val lux = event.values[0]
}
override fun onResume() {
super.onResume()
mLight?.also { light ->
sensorManager.registerListener(this, light, SensorManager.SENSOR_DELAY_NORMAL)
}
}
override fun onPause() {
super.onPause()
sensorManager.unregisterListener(this)
}
}
Tried to build the program after the instruction. Also watched some YouTube Tutorials, but I don't understand, and thay didn't worked for me.
CodePudding user response:
If I'm not mistaken you might be using this as your resource, and if you take a closer look:
override fun onSensorChanged(event: SensorEvent) {
// The light sensor returns a single value.
// Many sensors return 3 values, one for each axis.
val lux = event.values[0]
// Do something with this sensor value.
}
it is different than your implementation:
override fun onSensorChanged(p0: SensorEvent?) {
val lux = event.values[0]
}
just rename the p0
parameter to event
CodePudding user response:
Copy my onSensorChanged method and replace with Your
override fun onSensorChanged(p0: SensorEvent?) {
val lux = p0.values[0]
}