问题:


将id为“ readOut”的textView设置为wifi信号强度的值测试


码:

class wifi {
    int signalStrength = 0;
    int loopToggle = 0;
    Context context = MainActivity.this;

    @RequiresApi(api = Build.VERSION_CODES.ICE_CREAM_SANDWICH)
    public void loop() throws InterruptedException {
        while (loopToggle == 0) {
            WifiManager signalStrength = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
            String wifiInfo = WifiManager.EXTRA_WIFI_INFO;
            TextView textView = (TextView) textView.findViewById(readOut);
            Thread.sleep(1000);
        }
    }
}


布局文件:

<?xml version="1.0" encoding="utf-8"?>
<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="com.example.wifilocator.MainActivity">

    <TextView
        android:id="@+id/readOut"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="filler"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>


感谢您的任何帮助,您可以提供!

最佳答案

如果您使用xml属性R.id.readOut引用View,则必须编写android:id="@+id/readOut"

所以整行应该是

TextView textView = (TextView) textView.findViewById(R.id.readOut);


R代表R类,该类将生成以包含您在应用程序中所需的各种资源。

08-04 00:05