有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java无法在Android Studio 3.0 Canary 8中解析符号“Value”

我是安卓新手,目前面临一些错误。其中之一就是

无法解析符号“值”

enter image description here

“值”以红色突出显示,生成失败。我猜我在java中犯了一个简单的错误,但是坐了几天之后,我没有看到它

以下是主要活动。java代码:

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;

    }


}}

如果您能快速查看一下语法是否有错误,我将非常感激


共 (1) 个答案

  1. # 1 楼答案

    声明值变量,如下所示-

    private int value;
    

    在你的onCreate方法之上。变量声明是必须的,然后才能在代码中的任何地方使用它。如果将其设置为全局变量,则可以从onClick方法访问它,也可以从外部的任何其他地方访问它