我试图用我在youtube上发现的本教程构建这个简单的计算器,但我不断收到“错误找不到符号变量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>

最佳答案

您必须在“ onCreate”方法中初始化所有editText,Button等。当活动开始时,首先调用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+"");

}

10-07 19:16