Search code examples
androidnullpointerexceptionandroid-togglebutton

How to use ToggleButtons to determine what Counter to Add to


I have two integer variables which acts as a Counters and a button. I also have 2 Toggle Buttons which are linked so only 1 of them will be active at a time.When the Add button is clicked, i want to add 1 to the counter variable,based on the toggle button checked; ie if toggle1 is checked, 1 gets added to counter1

I've tried it like this with no joy. Any help would be great.

Counter1= 0;
Counter2= 0;
Add1 = (Button) findViewById(R.id.add1Button);
toggle1 = (ToggleButton) findViewById(R.id.toggle1Button);
toggle2 = (ToggleButton) findViewById(R.id.toggle2Button);
counter1Display= (TextView) findViewById(R.id.count1);
counter2Display= (TextView) findViewById(R.id.count2);

Add1.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
    if (toggle1.isChecked()) {
        Counter1 += 1;
        counter1Display.setText("" + Counter1);

    }
    if (toggle2.isChecked()) {
        Counter2 += 1;
        counter2Display.setText("" + Counter2);
    }
}
});

Here's the LogCat

07-28 11:44:01.156: D/AndroidRuntime(320): Shutting down VM
07-28 11:44:01.156: W/dalvikvm(320): threadid=1: thread exiting with uncaught exception (group=0x40015560)
07-28 11:44:01.176: E/AndroidRuntime(320): FATAL EXCEPTION: main
07-28 11:44:01.176: E/AndroidRuntime(320): java.lang.NullPointerException
07-28 11:44:01.176: E/AndroidRuntime(320):  at com.kh.counterapp.MainActivity$2.onClick(MainActivity.java:90)
07-28 11:44:01.176: E/AndroidRuntime(320):  at android.view.View.performClick(View.java:2485)
07-28 11:44:01.176: E/AndroidRuntime(320):  at android.view.View$PerformClick.run(View.java:9080)
07-28 11:44:01.176: E/AndroidRuntime(320):  at android.os.Handler.handleCallback(Handler.java:587)
07-28 11:44:01.176: E/AndroidRuntime(320):  at android.os.Handler.dispatchMessage(Handler.java:92)
07-28 11:44:01.176: E/AndroidRuntime(320):  at android.os.Looper.loop(Looper.java:123)
07-28 11:44:01.176: E/AndroidRuntime(320):  at android.app.ActivityThread.main(ActivityThread.java:3683)
07-28 11:44:01.176: E/AndroidRuntime(320):  at java.lang.reflect.Method.invokeNative(Native Method)
07-28 11:44:01.176: E/AndroidRuntime(320):  at java.lang.reflect.Method.invoke(Method.java:507)
07-28 11:44:01.176: E/AndroidRuntime(320):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
07-28 11:44:01.176: E/AndroidRuntime(320):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
07-28 11:44:01.176: E/AndroidRuntime(320):  at dalvik.system.NativeStart.main(Native Method)

Solution

  • Chcek this code..

        counter1=0;
        counter2=0;
        tg1 = (ToggleButton) findViewById(R.id.toggleButton1);
        tg2 = (ToggleButton) findViewById(R.id.toggleButton2);
        bt1 = (Button) findViewById(R.id.button1);
        counter1Display= (TextView) findViewById(R.id.count1);
        counter2Display= (TextView) findViewById(R.id.count2);
    
    
        tg1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
    
            @Override
            public void onCheckedChanged(CompoundButton v,
                    boolean isChecked) {
                if (tg2.isChecked()) {
                    tg2.setChecked(false);
                }
            }
        });
        tg2.setOnCheckedChangeListener(new OnCheckedChangeListener() {
    
            @Override
            public void onCheckedChanged(CompoundButton v,
                    boolean isChecked) {
                if (tg1.isChecked()) {
                    tg1.setChecked(false);
                }
            }
        });
    
        bt1.setOnClickListener(new OnClickListener() {
    
            @Override
            public void onClick(View v) {
                if (tg1.isChecked()) {
                    counter1=counter1+1;
                    counter1Display.setText(""+counter1);
    
                } else if (tg2.isChecked()) {
                    counter2=counter2+1;
                    counter2Display.setText(""+counter2);
                }
    
            }
        });