英文:
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("BUTTON", "Tapped!");
if (isAppRunning) { //If the app is running, stop 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 app
//some code
isAppRunning = true;
view.setBackgroundColor(getColor(R.color.colorRed));
view.setText("Stop Reminder"); //There's an error here
//some more code
}
}
This is the XML for the button:
<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 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论