【发布时间】:2019-11-05 13:33:47
【问题描述】:
我正在为一个小游戏制作一个计算器,在我添加了一个切换按钮之前一切正常。该应用程序在启动时崩溃,但我找不到任何错误。
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" />
.
.
.
【问题讨论】:
-
错误信息是什么?
-
java.lang.RuntimeException:无法实例化活动 ComponentInfo{com.example.android.detailedbalotcalculator/com.example.android.detailedbalotcalculator.BalotCalculator}:java.lang.NullPointerException:尝试调用虚拟方法'android.content.res.Resources android.content.Context.getResources()' 在空对象引用上
-
是否有直接引用您的代码的引用?这种方式几乎不可能帮助到你
-
对不起,我刚接触编码,有什么可以向您解释我的问题?引用代码没有直接错误,我不允许将 logcat 粘贴到问题中,因为它比文字更多。
-
你可以使用github上的Gist,然后把链接放在这里:gist.github.com或者不使用账号pastebin.com
标签: java macos android-studio crash togglebutton