0

I have a whole line of switch cases tied to oncheckboxclickeds that I'm using to total up a number.

Every case in the line works beautifully, except the last one, which seems not to do anything at ALL, and I can't figure out why.

This works just fine, right till the end:

package com.example.blazingboltcalculator;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;


public class MainActivity extends Activity {



int attack=11;
int damage=3;
int flankAttack;
int pbs;
int justice;
int destruction;
int bane;
int DF;
int GMW;
int heroism;
int deadly;
int attackTotal;
int damageTotal;

String flames="";
String baneDice="";
String keen="";
String diceTotal;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}




public void onCheckboxClicked(View v) {
    // Is the view now checked?
    boolean checked = ((CheckBox) v).isChecked();


    // Check which checkbox was clicked
    switch(v.getId()) {

    case R.id.checkBox1:
        if (checked)
                {
                flankAttack=2;
                attackTotal=attack+flankAttack+pbs+justice+bane+DF+GMW+heroism+deadly;
                damageTotal=damage+pbs+destruction+bane+DF+GMW+deadly;

                TextView textView = (TextView) findViewById(R.id.textView2);
                TextView textView2 = (TextView) findViewById(R.id.textView4);
                textView.setText(Integer.toString(attackTotal));
                textView2.setText(Integer.toString(damageTotal));
                }

        else
                {
                flankAttack=0;
                attackTotal=attack+flankAttack+pbs+justice+bane+DF+GMW+heroism+deadly;
                damageTotal=damage+pbs+destruction+bane+DF+GMW+deadly;

                TextView textView = (TextView) findViewById(R.id.textView2);
                TextView textView2 = (TextView) findViewById(R.id.textView4);
                textView.setText(Integer.toString(attackTotal));
                textView2.setText(Integer.toString(damageTotal));
                }
        break;

    case R.id.checkBox2:
        if (checked)
                {
                pbs=1; 
                attackTotal=attack+flankAttack+pbs+justice+bane+DF+GMW+heroism+deadly;
                damageTotal=damage+pbs+destruction+bane+DF+GMW+deadly;

                TextView textView = (TextView) findViewById(R.id.textView2);
                TextView textView2 = (TextView) findViewById(R.id.textView4);
                textView.setText(Integer.toString(attackTotal));
                textView2.setText(Integer.toString(damageTotal));
                }
        else
                {
                pbs=0;
                attackTotal=attack+flankAttack+pbs+justice+bane+DF+GMW+heroism+deadly;
                damageTotal=damage+pbs+destruction+bane+DF+GMW+deadly;

                TextView textView = (TextView) findViewById(R.id.textView2);
                TextView textView2 = (TextView) findViewById(R.id.textView4);
                textView.setText(Integer.toString(attackTotal));
                textView2.setText(Integer.toString(damageTotal));
                }
        break;

    case R.id.checkBox3:
            if (checked)
            {
                justice=2;
                attackTotal=attack+flankAttack+pbs+justice+bane+DF+GMW+heroism+deadly;
                damageTotal=damage+pbs+destruction+bane+DF+GMW+deadly;

                TextView textView = (TextView) findViewById(R.id.textView2);
                TextView textView2 = (TextView) findViewById(R.id.textView4);
                textView.setText(Integer.toString(attackTotal));
                textView2.setText(Integer.toString(damageTotal));
               }
            else
            {
                justice=0;
                attackTotal=attack+flankAttack+pbs+justice+bane+DF+GMW+heroism+deadly;
                damageTotal=damage+pbs+destruction+bane+DF+GMW+deadly;

                TextView textView = (TextView) findViewById(R.id.textView2);
                TextView textView2 = (TextView) findViewById(R.id.textView4);
                textView.setText(Integer.toString(attackTotal));
                textView2.setText(Integer.toString(damageTotal));
                }
            break;

        case R.id.checkBox4:
            if (checked)
            {
                destruction=4;
                attackTotal=attack+flankAttack+pbs+justice+bane+DF+GMW+heroism+deadly;
                damageTotal=damage+pbs+destruction+bane+DF+GMW+deadly;

                TextView textView = (TextView) findViewById(R.id.textView2);
                TextView textView2 = (TextView) findViewById(R.id.textView4);
                textView.setText(Integer.toString(attackTotal));
                textView2.setText(Integer.toString(damageTotal));
                }
            else
            {
                destruction=0;
                attackTotal=attack+flankAttack+pbs+justice+bane+DF+GMW+heroism+deadly;
                damageTotal=damage+pbs+destruction+bane+DF+GMW+deadly;

                TextView textView = (TextView) findViewById(R.id.textView2);
                TextView textView2 = (TextView) findViewById(R.id.textView4);
                textView.setText(Integer.toString(attackTotal));
                textView2.setText(Integer.toString(damageTotal));
                }
            break;

        case R.id.checkBox5:
            if (checked)
            {
                bane=2;
                attackTotal=attack+flankAttack+pbs+justice+bane+DF+GMW+heroism+deadly;
                damageTotal=damage+pbs+destruction+bane+DF+GMW+deadly;
                baneDice="+2d6";
                diceTotal="1d8"+baneDice+flames+keen;

                TextView textView = (TextView) findViewById(R.id.textView2);
                TextView textView2 = (TextView) findViewById(R.id.textView4);
                TextView textView3 = (TextView) findViewById(R.id.textView6);
                textView.setText(Integer.toString(attackTotal));
                textView2.setText(Integer.toString(damageTotal));
                textView3.setText(diceTotal);
                }
            else
            {
                bane=0;
                attackTotal=attack+flankAttack+pbs+justice+bane+DF+GMW+heroism+deadly;
                damageTotal=damage+pbs+destruction+bane+DF+GMW+deadly;
                baneDice="";
                diceTotal="1d8"+baneDice+flames+keen;

                TextView textView = (TextView) findViewById(R.id.textView2);
                TextView textView2 = (TextView) findViewById(R.id.textView4);
                TextView textView3 = (TextView) findViewById(R.id.textView6);
                textView.setText(Integer.toString(attackTotal));
                textView2.setText(Integer.toString(damageTotal));
                textView3.setText(diceTotal);
                }
            break;

        case R.id.checkBox6:
            if (checked)
            {
                DF=3;
                attackTotal=attack+flankAttack+pbs+justice+bane+DF+GMW+heroism+deadly;
                damageTotal=damage+pbs+destruction+bane+DF+GMW+deadly;

                TextView textView = (TextView) findViewById(R.id.textView2);
                TextView textView2 = (TextView) findViewById(R.id.textView4);
                textView.setText(Integer.toString(attackTotal));
                textView2.setText(Integer.toString(damageTotal));
                }
            else
            {
                DF=0;
                attackTotal=attack+flankAttack+pbs+justice+bane+DF+GMW+heroism+deadly;
                damageTotal=damage+pbs+destruction+bane+DF+GMW+deadly;

                TextView textView = (TextView) findViewById(R.id.textView2);
                TextView textView2 = (TextView) findViewById(R.id.textView4);
                textView.setText(Integer.toString(attackTotal));
                textView2.setText(Integer.toString(damageTotal));
                }
            break;

        case R.id.checkBox7:
            if (checked)
            {
                flames="+1d6 fire";
                diceTotal="1d8"+baneDice+flames+keen;
                TextView textView3 = (TextView) findViewById(R.id.textView6);
                textView3.setText(diceTotal);
            }
            else
            {
                flames="";
                diceTotal="1d8"+baneDice+flames+keen;
                TextView textView3 = (TextView) findViewById(R.id.textView6);
                textView3.setText(diceTotal);
            }
            break;

        case R.id.checkBox8:
            if (checked)
            {
                GMW=1;
                attackTotal=attack+flankAttack+pbs+justice+bane+DF+GMW+heroism+deadly;
                damageTotal=damage+pbs+destruction+bane+DF+GMW+deadly;

                TextView textView = (TextView) findViewById(R.id.textView2);
                TextView textView2 = (TextView) findViewById(R.id.textView4);
                textView.setText(Integer.toString(attackTotal));
                textView2.setText(Integer.toString(damageTotal));
                }
            else
            {
                GMW=0;
                attackTotal=attack+flankAttack+pbs+justice+bane+DF+GMW+heroism+deadly;
                damageTotal=damage+pbs+destruction+bane+DF+GMW+deadly;

                TextView textView = (TextView) findViewById(R.id.textView2);
                TextView textView2 = (TextView) findViewById(R.id.textView4);
                textView.setText(Integer.toString(attackTotal));
                textView2.setText(Integer.toString(damageTotal));
                }
            break;

        case R.id.checkBox9:
            if (checked)
            {
                heroism=2;
                attackTotal=attack+flankAttack+pbs+justice+bane+DF+GMW+heroism+deadly;
                damageTotal=damage+pbs+destruction+bane+DF+GMW+deadly;

                TextView textView = (TextView) findViewById(R.id.textView2);
                TextView textView2 = (TextView) findViewById(R.id.textView4);
                textView.setText(Integer.toString(attackTotal));
                textView2.setText(Integer.toString(damageTotal));
               }
            else
            {
                heroism=0;
                attackTotal=attack+flankAttack+pbs+justice+bane+DF+GMW+heroism+deadly;
                damageTotal=damage+pbs+destruction+bane+DF+GMW+deadly;

                TextView textView = (TextView) findViewById(R.id.textView2);
                TextView textView2 = (TextView) findViewById(R.id.textView4);
                textView.setText(Integer.toString(attackTotal));
                textView2.setText(Integer.toString(damageTotal));
                }
            break;

        case R.id.checkBox10:
            if (checked)
            {
                keen="+19-20 crit";
                diceTotal="1d8"+baneDice+flames+keen;
                TextView textView3 = (TextView) findViewById(R.id.textView6);
                textView3.setText(diceTotal);
            }
            else
            {
                keen="";
                diceTotal="1d8"+baneDice+flames+keen;
                TextView textView3 = (TextView) findViewById(R.id.textView6);
                textView3.setText(diceTotal);
            }
            break;

        case R.id.checkBox11:
            if (checked)
            {
                deadly=2;
                attackTotal=attack+flankAttack+pbs+justice+bane+DF+GMW+heroism+deadly;
                damageTotal=damage+pbs+destruction+bane+DF+GMW+deadly;

                TextView textView = (TextView) findViewById(R.id.textView2);
                TextView textView2 = (TextView) findViewById(R.id.textView4);
                textView.setText(Integer.toString(attackTotal));
                textView2.setText(Integer.toString(damageTotal));
            }
            else
            {
                deadly=0;
                attackTotal=attack+flankAttack+pbs+justice+bane+DF+GMW+heroism+deadly;
                damageTotal=damage+pbs+destruction+bane+DF+GMW+deadly;

                TextView textView = (TextView) findViewById(R.id.textView2);
                TextView textView2 = (TextView) findViewById(R.id.textView4);
                textView.setText(Integer.toString(attackTotal));
                textView2.setText(Integer.toString(damageTotal));
            }
    }
}

}

I'm kinda new to programming, does anyone have any idea what might be gumming up the works? The whole mess works perfectly aside from that very last Case.

4

1 回答 1

1

Well, we would need a bit more information to help you. However, I assume you are trying to use the OnCheckedChangeListener which is implemented by your main Activity? If you are having trouble with referencing IDs, you can always just implement an individual lisenter on a specific element, for example:

checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener()
   {
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
        {
            if ( isChecked )
            {

                prevChecked = true;
        Log.d("CHECKBOX","Previously Checked = "+prevChecked);
            }else{

                prevChecked = false;
        Log.d("CHECKBOX","Previously Checked = "+prevChecked);

            }

        }
    });

Where prevChecked is a global variable. I have also thrown in a couple of Log's so you can output the information to the LogCat. Hope that helps in your debugging.

***** EDIT TO SUIT YOUR ADDED COMMENT *************

Have you tried to swap your switch statement with and if statement. e.g.

if(v.getId() == R.id.checkbox1){

}else if(v.getId() == R.id.checkbox2){

}

Additionally, what interface is onCheckboxClicked(View v), is this a custom element? How have you defined your check boxes in your R.layout.activity_main.xml? If you have a checkbox interface you need to implement a listener like my example above. I can't see that your setting a listener anywhere, nor are you implementing it in your activity. For example:

public class MainActivity extends Activity implements onCheckChangedListener {

But as far as I can tell, unless it is a custom element it doesn't make any sense that public void onCheckboxClicked(View v) Even get's called. This example might help.

于 2012-07-30T03:28:30.393 回答