我正在为迷你游戏制作计算器,直到我添加了toggleButton之前,一切正常。该应用程序在启动时崩溃,但是我找不到任何错误。
JAVA代码:
.
.
.
int colorOff = getResources().getcolor(R.color.toggleOff);
int colorOn = getResources().getcolor(R.color.toggleon);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_balot_calculator); }
//This method is supposed to change the color of the toggle button when it is checked and not checked.
public void seraA(View view) {
ToggleButton seraToggle = (ToggleButton) findViewById(R.id.sera_toggle);
if (seraToggle.isChecked()) {
seraToggle.setBackgroundColor(colorOn); }
else
seraToggle.setBackgroundColor(colorOff);
}
.
.
.
XML代码:
.
.
.
<ToggleButton
android:id="@+id/sera_toggle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="8dp"
android:layout_marginVertical="4dp"
android:onClick="seraA"
android:textOn="@string/sera_T"
android:textOff="@string/sera_T"
android:textColor="#E0F7FA"
android:textSize="20sp" />
.
.
.