2017-08-03 74 views
0

我是新来的android,目前面临一些错误。其中之一是无法解析Android Studio 3.0中的符号'Value'Canary 8

无法解析符号“价值”

enter image description here

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

这里的MainActivity.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; 

    } 


}} 

如果你能快速浏览一下,看看是否有一个语法错误,我会非常感激。

+0

由于您是Android新手,因此这个值为零。这个错误通常在Java中或者在很多语言中被诚实抛出。您的变量'value'永远不会被声明 –

+0

实例化'value'变量。 'int value = 0;' – Bajal

回答

0

声明变量的值,如下面 -

private int value; 

以上的onCreate方法。您可以在代码中的任何位置使用变量声明。如果您将其设为全局变量,则可以通过onClick方法访问它,并且还可以在其他任何地方访问它。

+0

非常感谢,解决了。 –