0
votes

I'm new to android, currently facing a handful of errors. One of which is

Cannot resolve symbol 'Value'

enter image description here

'Value' is highlighted in red, and the build is failed. I'm guessing I've made a simple error in java, but after a few days of sitting on it, I am not seeing it.

Here's the MainActivity.java code:

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

private Button btnAdd;
private Button btnTake;
private TextView txtValue;
private Button btnGrow;
private Button btnShrink;
private Button btnReset;
private Button btnHide;

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

    // get reference to all buttons in UI. Match them to all declared Button objects
    btnAdd = (Button) findViewById(R.id.btnAdd);
    btnTake = (Button) findViewById(R.id.btnTake);
    txtValue = (TextView) findViewById(R.id.txtValue);
    btnGrow = (Button) findViewById(R.id.btnGrow);
    btnShrink = (Button) findViewById(R.id.btnShrink);
    btnReset = (Button) findViewById(R.id.btnReset);
    btnHide = (Button) findViewById(R.id.btnHide);

    // listen for all the button clicks
    btnAdd.setOnClickListener(this);
    btnTake.setOnClickListener(this);
    txtValue.setOnClickListener(this);
    btnGrow.setOnClickListener(this);
    btnShrink.setOnClickListener(this);
    btnReset.setOnClickListener(this);
    btnHide.setOnClickListener(this);
}

@Override
public void onClick(View view) {

    // a local variable to use later
    float size;

    switch (view.getId()){

        // case 1
        case  R.id.btnAdd:
            value++;
            txtValue.setText(""+ value);

            break;

        // case 2
        case  R.id.btnTake:
            value--;
            txtValue.setText(""+ value);

            break;

        // case 3
        case R.id.btnReset:
            value = 0;
            txtValue.setText(""+ value);

            break;

        // case 4
        case R.id.btnGrow:
            size = txtValue.getTextScaleX();
            txtValue.setTextScaleX(size + 1);

            break;

        // case 5
        case R.id.btnShrink:
            size = txtValue.getTextScaleX();
            txtValue.setTextScaleX(size - 1);

            break;

        // last case statement with if-else
        case R.id.btnHide:
            if (txtValue.getVisibility() == View.VISIBLE){

                // currently visible so hide it
                txtValue.setVisibility(View.INVISIBLE);

                // change text on the button
                btnHide.setText("SHOW");
            }else{
                // hidden so show
                txtValue.setVisibility(View.VISIBLE);

                // change text on button
                btnHide.setText("HIDE");
            }

            break;

    }


}}

If you can take a quick look to see whether there's a mistake in syntax, I would be very thankful.

1
This has zero to due w/the fact that you are new to Android. This error is thrown in Java in general, or in a lot of languages, honestly. Your variable value is never declaredRabbit Guy
Instantiate the value variable. int value=0;Bajal

1 Answers

0
votes

Declare the value variable like below -

private int value;

Above your onCreate method. Variable declaration is required before you can use it anywhere in the code. If you make it Global variable you can access it from onClick method and also outside anywhere else as well.