【问题标题】:Cannot find declaration to go to in Android Studio在 Android Studio 中找不到要访问的声明
【发布时间】:2021-01-20 13:03:59
【问题描述】:

我制作了一个使用手机传感器的应用。如果我将 TextView 改回 textView.setText 比 textView 不起作用,但是当我将它改回 TextView.SetTextsettext 部分不起作用。

这是我的 MainActivity.kt:

    var sm = getSystemService(Context.SENSOR_SERVICE) as SensorManager
    var list = sm.getSensorList(Sensor.TYPE_ACCELEROMETER)
    var se = object : SensorEventListener {
        override fun onAccuracyChanged(p0: Sensor?, p1: Int) {
        }

        override fun onSensorChanged(sensorEvent: SensorEvent?) {
            var values = sensorEvent?.values
            var x = values?.get(0)
            var y = values?.get(1)
            var z = values?.get(2)
            TextView.SetText("X=$x\nY=$y\nZ=$z")
        }
    }
    sm.registerListener(se,list.get(0), SensorManager.SENSOR_DELAY_NORMAL)

这是我的 activity_main.xml

<TextView
    android:id="@+id/textView.TextView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello World!"
    android:textStyle="bold"
    android:textColor="@color/black"
    android:textSize="30sp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

感谢社区提供的任何建议。谢谢。

【问题讨论】:

    标签: xml android-studio kotlin


    【解决方案1】:

    首先,请在你的xml中更改你TextView的id

    android:id="@+id/textView.TextView"  
    

    名字真的错了。使用类似“@+id/text_view”的东西

    之后我认为在 Kotlin 中你不能使用 setTextSetText.. 请不要这样使用:

    textView.text="X=$x\nY=$y\nZ=$z"
    

    【讨论】:

    • 感谢您的反应,我已经尝试过了,但现在 .text 不起作用。你有什么解决这个问题的办法。先感谢您。 TextView.text="X=$x\nY=$y\nZ=$z"
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多