user3041073
user3041073

Reputation: 33

android Local variable not used from Switch

Why I get two warnings: that variable "input5" is not used, and when I want to use it later in Intent: "input5" cannot be resolved to variable.

What am I missing in between? Thanks!

    final Switch mySwitch = (Switch) findViewById(R.id.edit_home);
    mySwitch.setOnCheckedChangeListener(new Switch.OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView,
                boolean isChecked) {
            // TODO Auto-generated method stub
            if(isChecked){
                 **double input5 = 1;**
                }else{
                 **double input5 = 2;**
                }

    Button buttonForward = (Button) findViewById(R.id.buttonToMain2);
    buttonForward.setOnClickListener(new OnClickListener(){

        @Override
        public void onClick(View arg0) {


    EditText edit_team = (EditText) findViewById(R.id.edit_team);
    EditText edit_form = (EditText) findViewById(R.id.edit_form);
    EditText edit_import = (EditText) findViewById(R.id.edit_import);       
    String ekipa1 = edit_team.getText().toString();

    final double input2 = Double.valueOf(ratingBar.getRating()); 
    final double input3 = Double.valueOf(edit_form.getText().toString());
    final double input4 = Double.valueOf(mplayerControl.getProgress());
    final double input6 = Double.valueOf(edit_import.getText().toString());

    Intent intent=new Intent(MainActivity.this,Main2Activity.class);
    intent.putExtra("Value",input2);
    intent.putExtra("Value1",input3);
    intent.putExtra("Value2",input4);
    intent.putExtra("Value3",**input5**);
    intent.putExtra("Value4",input6);
    intent.putExtra("team1", ekipa1);
    startActivity(intent);


            }       
    });
    }
    });     

Upvotes: 0

Views: 101

Answers (3)

Raghunandan
Raghunandan

Reputation: 133560

Declare input5 as a instance-member

  if(isChecked){
             **double input5 = 1;**
            }else{
             **double input5 = 2;**
            }

It is declared and initialized in the if else block (input 5 is local to if else block).

If you have already declared as instance-member then initialize the variable input5.

 if(isChecked){
             input5 = 1;
            }else{
             input5 = 2;
            }

Upvotes: 1

AlexS
AlexS

Reputation: 5345

All your variables are declared in a method, which means they can only be accessed inside this method.

Therefor input2, inpu3, input4 and input6 are only accessable in your onClick() and input5 is only accessable inside onCheckedChanged().

Try this:

final Switch mySwitch = (Switch) findViewById(R.id.edit_home);

final double input2 = Double.valueOf(ratingBar.getRating()); 
final double input3 = Double.valueOf(edit_form.getText().toString());
final double input4 = Double.valueOf(mplayerControl.getProgress());
final double input5 = (mySwitch.isChecked() ? 1.0 : 2.0);
final double input6 = Double.valueOf(edit_import.getText().toString());

Upvotes: 0

ksasq
ksasq

Reputation: 4412

The variable is going out of scope. I don't think that you need to create the onCheckChanged listener like you are at the moment. You can query the isChecked() function on the Switch directly from your Button's onClick listener, when you are creating the Intent.

Upvotes: 0

Related Questions