Home > Enterprise >  Android, Kotlin and XLM are not sync
Android, Kotlin and XLM are not sync

Time:06-09

I think the Kotlin and the XML should be sync; however, it is not. For the Kotlin, It does not recognize, and it wants to do a variant.

Kotlin: stepsValue.setText("" event.values[0]) On my Activity_Main for the XML: android:id="@ id/stepsValue"

The error: Unresolved reference: stepsValue The error seems like the Kotlin and XML are not sync or I forget something? Here is all code:

Kotlin:


import android.content.Context
import android.hardware.Sensor
import android.hardware.SensorEvent
import android.hardware.SensorEventListener
import android.hardware.SensorManager
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast

class MainActivity : AppCompatActivity(), SensorEventListener {

    var running = false
    var sensorMangage:SensorManager? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        sensorMangage = getSystemService(Context.SENSOR_SERVICE) as SensorManager
    }

    override fun onResume(){
        super.onResume()
        running = true
        var stepsSensor = sensorMangage?.getDefaultSensor(Sensor.TYPE_STEP_COUNTER)
        if (stepsSensor == null) {
            Toast.makeText( this, "No Step Counter Sensor !", Toast.LENGTH_SHORT).show()
        } else {
            sensorMangage?.registerListener(this, stepsSensor, SensorManager.SENSOR_DELAY_UI)
        }

    }

    override fun onPause() {
        super.onPause()
        running = false
        sensorMangage?.unregisterListener(this)
    }

    override fun onAccuracyChanged(sensor: Sensor?, accuracy: Int) {
    }

    override fun onSensorChanged(event: SensorEvent) {
        if (running) {
            stepsValue.setText(""   event.values[0])
        }
    }


} 

XML:

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@ id/stepsLbl"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="92dp"
        android:text="Steps"
        android:textSize="50sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.498"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@ id/stepsValue"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="56dp"
        android:text="0"
        android:textSize="35sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.498"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@ id/stepsLbl" />

</android.support.constraint.ConstraintLayout>

CodePudding user response:

If you want to access a view defined in the XML file from the activity code, you have to retrieve it and declare a variable to store it in - it's not automatically defined (unless you use view binding, then it is sort of automatically defined, or are using synthetics, which are deprecated).

To get a view by its ID in an activity, you can use findViewById like this:

val stepsValue = findViewById<TextView>(R.id.stepsValue)
stepsValue.setText("some text")

You can only call this after your activity has called setContentView in onCreate, so you cannot use this in a class member variable declaration. If you want to save it as a class member, you can do so in onCreate then use it throughout the activity, like this:

// don't do this!
// private val stepsValue = findViewById<TextView>(R.id.stepsValue)

//do this instead
private lateinit var stepsValue: TextView

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    stepsValue = findViewById(R.id.stepsValue)
}

If you get tired of manually adding a ton of findViewById calls as the app grows, look at using view binding, which does that part automatically for you.

  • Related