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

时间:2013-03-28 21:43:10

标签: java android

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

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);
         }
            }
     };

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

这是logcat:

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

1 个答案:

答案 0 :(得分:2)

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

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