2015-08-30 127 views
0

我試圖建立這個簡單的計算器關閉本教程我發現在YouTube上,但我不斷收到「錯誤找不到符號變量ID」,「錯誤找不到符號變量菜單」和「錯誤找不到符號佈局」和id,菜單和佈局以紅色顯示這裏是代碼,並請幫忙,謝謝您:錯誤找不到符號變量id

package com.example.khalil.basiccalculator; 

import android.support.v7.app.ActionBarActivity; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.widget.EditText; 
import android.widget.TextView; 


public class MainActivity extends ActionBarActivity { 

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

    public void onButtonClick(View v) 
    { 
     //2 text fields - Edit Text 
     EditText a1 = (EditText)findViewById(R.id.Tfnum1); 
     EditText a2 = (EditText)findViewById(R.id.Tfnum2); 

     //1 result label - TextView 
     TextView tv = (TextView) findViewById(R.id.Lresult); 
     boolean flag = false; //no division by 0 error 
     double num1,num2,ans; 
     num1 = Double.parseDouble(a1.getText().toString()); 
     num2 = Double.parseDouble(a2.getText().toString()); 

     if (v.getId() == R.id.Badd) 
      ans = num1 + num2; 
     if(v.getId() == R.id.Bsub) 
      ans = num1 - num2; 
     if(v.getId() == R.id.Bmult) 
      ans = num1 * num2; 
     if(v.getId() == R.id.Bdiv) 
      //division by 0 
      if (num2 == 0) 
       flag = true; //division by 0 error 
      else 
       ans = num1/num2; 

     tv.setText(ans+""); 

    } 


    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.menu_main, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 

     //noinspection SimplifiableIfStatement 
     if (id == R.id.action_settings) { 
      return true; 
     } 

     return super.onOptionsItemSelected(item); 
    } 

    private class R { 
    } 
} 

而且繼承人的XML:

  <?xml version="1.0"?> 
<LinearLayout 
    android:id="@+id/activity_main" 
    tools:context=".MainActivity" 
    android:paddingBottom="@dimen/activity_vertical_margin"   
    android:orientation="vertical" 
    android:paddingTop="@dimen/activity_vertical_margin"  
    android:paddingRight="@dimen/activity_horizontal_margin"  
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:layout_height="match_parent" android:layout_width="match_parent"  
    xmlns:tools="http://schemas.android.com/tools" 
    xmlns:android="http://schemas.android.com/apk/res/android"> 

<TextView 
    android:id="@+id/Lnum1" 
    android:layout_height="wrap_content" 
    android:layout_width="wrap_content" 
    android:text="Enter First Number" android:textAppearance="? 
    android:attr/textAppearanceMedium"/> 

<EditText 
    android:id="@+id/Tfnum1" 
    android:layout_height="wrap_content" 
    android:layout_width="match_parent"/> 

<TextView 
    android:id="@+id/Lnum2" 
    android:layout_height="wrap_content" 
    android:layout_width="wrap_content" 
    android:text="Enter Second Number" 
    android:textAppearance="?android:attr/textAppearanceMedium"/> 

<EditText 
    android:id="@+id/Tfnum2" 
    android:layout_height="wrap_content" 
    android:layout_width="match_parent"/> 

<TextView 
    android:id="@+id/Lnum3" 
    android:layout_height="wrap_content" 
    android:layout_width="wrap_content" 
    android:text="@string/emptystring" android:textAppearance="? 
    android:attr/textAppearanceLarge"/> 

<Button 
    android:id="@+id/Badd" 
    android:layout_height="wrap_content" 
    android:layout_width="wrap_content" 
    android:text="+" 
    android:onClick="onButtonClick" 
    style="?android:attr/buttonStyleSmall"/> 

<Button 
    android:id="@+id/Bsub" 
    android:layout_height="wrap_content" 
    android:layout_width="wrap_content" 
    android:text="-" 
    android:onClick="onButtonClick" 
    style="?android:attr/buttonStyleSmall"/> 

<Button 
    android:id="@+id/Bmult" 
    android:layout_height="wrap_content" 
    android:layout_width="wrap_content" 
    android:text="*" 
    android:onClick="onButtonClick" 
    style="?android:attr/buttonStyleSmall"/> 

<Button 
    android:id="@+id/Bdiv" 
    android:layout_height="wrap_content" 
    android:layout_width="wrap_content" 
    android:text="/"  
    android:onClick="onButtonClick" 
    style="?android:attr/buttonStyleSmall"/> 

</LinearLayout> 

回答

0

您必須初始化所有editTexts,按鈕等....在「onCreate」方法中。當活動開始的,首先的onCreate調用,所以無論何時訪問UI元素,用來初始化它在你的onCreate方法

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    //2 text fields - Edit Text 
    EditText a1 = (EditText)findViewById(R.id.Tfnum1); 
    EditText a2 = (EditText)findViewById(R.id.Tfnum2); 

    //1 result label - TextView 
    TextView tv = (TextView) findViewById(R.id.Lresult); 
    boolean flag = false; //no division by 0 error 
    double num1,num2,ans; 
    num1 = Double.parseDouble(a1.getText().toString()); 
    num2 = Double.parseDouble(a2.getText().toString()); 

    if (v.getId() == R.id.Badd) 
     ans = num1 + num2; 
    if(v.getId() == R.id.Bsub) 
     ans = num1 - num2; 
    if(v.getId() == R.id.Bmult) 
     ans = num1 * num2; 
    if(v.getId() == R.id.Bdiv) 
     //division by 0 
     if (num2 == 0) 
      flag = true; //division by 0 error 
     else 
      ans = num1/num2; 

    tv.setText(ans+""); 

} 
+0

謝謝你,這可能是一個愚蠢的問題,但我究竟該怎麼辦? –