为什么当我尝试在按钮上使用setText()时,Android Studio会显示错误?

huangapple go评论63阅读模式
英文:

Why is Android Studio showing an error when I try to use setText() on a Button?

问题

public void changeAppState(View view) {
    view = (Button) changeAppStateButton;
    Log.i("BUTTON", "Tapped!");
    if (isAppRunning) { //If the app is running, stop the app
        isAppRunning = false;
        view.setBackgroundColor(getColor(R.color.colorPurple));
        view.setText("Start Reminder"); //There's an error here
        timer.cancel();
        Log.i("TIMER", "Timer interrupted");

    } else { //If the app is not running, start the app
        //some code

        isAppRunning = true;
        view.setBackgroundColor(getColor(R.color.colorRed));
        view.setText("Stop Reminder"); //There's an error here

        //some more code
    }
}
<Button
    android:id="@+id/changeAppStateButton"
    android:layout_width="0dp"
    android:layout_height="45dp"
    android:layout_marginStart="16dp"
    android:layout_marginEnd="16dp"
    android:layout_marginBottom="110dp"
    android:background="#9C27B0"
    android:fontFamily="@font/alegreya_sans_sc"
    android:onClick="changeAppState"
    android:text="Start Reminder"
    android:textAlignment="center"
    android:textColor="#FFFFFF"
    android:textSize="25sp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toStartOf="parent" />

Android Studio在该函数中的setText()两次调用都会有高亮标记。我看不到任何错误消息。
有人可以告诉我为什么函数中的setText()无法正常工作吗?如果需要更多代码,请告诉我。

英文:

I'm making an app where I'm trying to run a timer for as long as the phone is asleep/idle. The timer will stop as soon as the phone is turned on.
<br>Here's the (relevant) code in main.java:

public void changeAppState(View view) {
        view = (Button) changeAppStateButton;
        Log.i(&quot;BUTTON&quot;, &quot;Tapped!&quot;);
        if (isAppRunning) { //If the app is running, stop app
            isAppRunning = false;
            view.setBackgroundColor(getColor(R.color.colorPurple));
            view.setText(&quot;Start Reminder&quot;); //There&#39;s an error here
            timer.cancel();
            Log.i(&quot;TIMER&quot;, &quot;Timer interrupted&quot;);

        } else { //If the app is not running, start app
            //some code

            isAppRunning = true;
            view.setBackgroundColor(getColor(R.color.colorRed));
            view.setText(&quot;Stop Reminder&quot;); //There&#39;s an error here

            //some more code
        }
    } 

This is the XML for the button:

    &lt;Button
        android:id=&quot;@+id/changeAppStateButton&quot;
        android:layout_width=&quot;0dp&quot;
        android:layout_height=&quot;45dp&quot;
        android:layout_marginStart=&quot;16dp&quot;
        android:layout_marginEnd=&quot;16dp&quot;
        android:layout_marginBottom=&quot;110dp&quot;
        android:background=&quot;#9C27B0&quot;
        android:fontFamily=&quot;@font/alegreya_sans_sc&quot;
        android:onClick=&quot;changeAppState&quot;
        android:text=&quot;Start Reminder&quot;
        android:textAlignment=&quot;center&quot;
        android:textColor=&quot;#FFFFFF&quot;
        android:textSize=&quot;25sp&quot;
        app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
        app:layout_constraintEnd_toEndOf=&quot;parent&quot;
        app:layout_constraintHorizontal_bias=&quot;0.0&quot;
        app:layout_constraintStart_toStartOf=&quot;parent&quot; /&gt;

Android Studio highlights both occurrences of setText() in the function. I don't see any error message.
Can someone tell me why setText() isn't working in the function? Let me know if you need more of the code.

答案1

得分: 0

你对 view 的重新分配并不会将变量的类型从 View 更改为 Button,因为它会自动重新转换回 View

相反,您需要像这样初始化一个类型为 Button 的变量:

Button button = (Button) changeAppStateButton;

(或者,正如 @Ahtisham 指出的,如果您还没有对 change app state 按钮的引用)

Button button = findViewById(R.id.changeAppStateButton);

由于这个新变量的类型是 Button,您将能够访问它的 setText 方法。

英文:

Your reassigning of view doesn't change the type of the variable from View to Button as it gets re-cast automatically back to View.

Instead, you need to initialise a variable of type Button like so:

Button button = (Button) changeAppStateButton;

(or, as @Ahtisham pointed out, if you haven't already got a reference to the change app state button)

Button button = findViewById(R.id.changeAppStateButton);

Since this new variable is of type Button, you will be able to access its setText method.

huangapple
  • 本文由 发表于 2020年4月11日 01:30:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/61145521.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定