google-developer-training / android-advanced

Solution apps for the apps that students create as they work through the Advanced Android Development training course created by Google Developer Training.
Other
1.14k stars 1.09k forks source link

Advanced Android: [Lesson #][Step #][description] #33

Closed Productivix closed 3 years ago

Productivix commented 3 years ago

Describe the problem A clear and concise description of what the problem is. it seem to be written in Java and when you copy it in android studio, it converts it in Kotlin , with more or less success

In which lesson and step of the codelab can this issue be found? Lesson number + step number. (e.g., Lesson 1.1, Step 1.3) : Advanced Android 03.1: Getting sensor data

How to reproduce? What are the exact steps to reproduce the problem? just copy your code

Versions

  1. What version of Android Studio are you using?
  2. What API level are you targeting?

Additional information Add any other context about the problem here.

codelab: advanced-android

The best code I add is : class MainActivity : AppCompatActivity() { private var mSensorManager: SensorManager? = null

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    mSensorManager = this.getSystemService(Context.SENSOR_SERVICE) as SensorManager?
    val sensorList: List<Sensor> = mSensorManager.getSensorList(Sensor.TYPE_ALL)
Productivix commented 3 years ago

rather : class MainActivity : AppCompatActivity() { private var mSensorManager: SensorManager? = null override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) mSensorManager = this.getSystemService(SENSOR_SERVICE) as SensorManager? val sensorList: List = mSensorManager.getSensorList(Sensor.TYPE_ALL) val sensorText = StringBuilder() for (currentSensor in sensorList) { sensorText.append(currentSensor.name).append( System.getProperty("line.separator")) } val sensorTextView = findViewById(R.id.sensor_list) as TextView sensorTextView.text = sensorText

}
Productivix commented 3 years ago

after debug it is : class MainActivity : AppCompatActivity() { private var mSensorManager: SensorManager? = null override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) mSensorManager = this.getSystemService(SENSOR_SERVICE) as SensorManager? val sensorList = mSensorManager?.getSensorList(Sensor.TYPE_ALL) val sensorText = StringBuilder() if (sensorList != null) { for (currentSensor in sensorList) { sensorText.append(currentSensor.name).append( System.getProperty("line.separator")) } } val sensorTextView = findViewById(R.id.sensor_list) as TextView sensorTextView.text = sensorText

}