gpt4 book ai didi

java - 将按钮的文本颜色更改为默认颜色时出错?

转载 作者:行者123 更新时间:2023-12-02 07:09:34 24 4
gpt4 key购买 nike

我有一个带有设置颜色代码的按钮:

buttonClicked.setTextColor(Color.GRAY);

现在,当我完成计时器时,所有按钮的文本都会重置为其他内容,因此我需要将该文本恢复为默认值。我尝试了这个,但出现错误(我在代码末尾附近为该错误添加了注释):

final OnClickListener clickListener = new OnClickListener() {

private Button buttonClicked;
private int brojacKlikova = 0;

public void onClick(View v) {
brojacKlikova++;
if (brojacKlikova < 16) {

Button button = (Button) v;
button.getBackground().setColorFilter(new LightingColorFilter(0xFFFFFFFF, 0x003333));

if (buttonClicked == null) {
// first button is clicked
buttonClicked = button;
} else {
// second button is clicked
if (buttonClicked.getTag().equals(button.getTag())) {
button.getBackground().setColorFilter(new LightingColorFilter(0xFFFFFFFF, 0x66FF33));
buttonClicked.getBackground().setColorFilter(new LightingColorFilter(0xFFFFFFFF, 0x66FF33));
buttonClicked.setEnabled(false);
button.setEnabled(false);
counter = counter + 5;
score.setText("Poeni: " + counter);
} else {
buttonClicked.setEnabled(false);
buttonClicked.setTextColor(Color.GRAY);
buttonClicked.getBackground().setColorFilter(new LightingColorFilter(0xFFFFFFFF, 0xFFCC99));
button.getBackground().clearColorFilter();
}
buttonClicked = null;
}
}else{
buttonClicked = null;
buttonClicked.setTextColor(Color.WHITE); //<--- here I get error
brojacKlikova = 0;
brojacVremena.cancel();
mHandler.postDelayed(mLaunchTask,2200);
}
}
};

如何将按钮的文本颜色重置为默认值?

这是日志猫:

03-28 23:59:29.357: W/dalvikvm(27918): threadid=1: thread exiting with uncaught exception (group=0x40018560)
03-28 23:59:29.467: E/AndroidRuntime(27918): FATAL EXCEPTION: main
03-28 23:59:29.467: E/AndroidRuntime(27918): java.lang.NullPointerException
03-28 23:59:29.467: E/AndroidRuntime(27918): at rs.androidaplikacije.spojnice.Spojnice$1.onClick(Spojnice.java:77)
03-28 23:59:29.467: E/AndroidRuntime(27918): at android.view.View.performClick(View.java:2506)
03-28 23:59:29.467: E/AndroidRuntime(27918): at android.view.View$PerformClick.run(View.java:9112)
03-28 23:59:29.467: E/AndroidRuntime(27918): at android.os.Handler.handleCallback(Handler.java:587)
03-28 23:59:29.467: E/AndroidRuntime(27918): at android.os.Handler.dispatchMessage(Handler.java:92)
03-28 23:59:29.467: E/AndroidRuntime(27918): at android.os.Looper.loop(Looper.java:130)
03-28 23:59:29.467: E/AndroidRuntime(27918): at android.app.ActivityThread.main(ActivityThread.java:3835)
03-28 23:59:29.467: E/AndroidRuntime(27918): at java.lang.reflect.Method.invokeNative(Native Method)
03-28 23:59:29.467: E/AndroidRuntime(27918): at java.lang.reflect.Method.invoke(Method.java:507)
03-28 23:59:29.467: E/AndroidRuntime(27918): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:864)
03-28 23:59:29.467: E/AndroidRuntime(27918): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:622)
03-28 23:59:29.467: E/AndroidRuntime(27918): at dalvik.system.NativeStart.main(Native Method)
03-28 23:59:31.167: I/Process(27918): Sending signal. PID: 27918 SIG: 9

最佳答案

buttonClicked = null;
buttonClicked.setTextColor(Color.WHITE);

问题就出在这里。您被分配给 NULL 按钮,然后调用它的属性。这总是抛出NPE。解决方案是删除第一行,它应该可以工作。

关于java - 将按钮的文本颜色更改为默认颜色时出错?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15692712/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com