2017-08-03 69 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

非常感謝,解決了。 –