无法解决 R.Id

Cant Resolve R.Id

问题:

可以在onCreate方法里面找到: 这里是说不能产生全部 R.id.XXXXX。 txtNumber1、txtNumber2、txtResult 都亮起红色,我不明白应该正确导入所有内容。我是 android 格式的新手,所以请详尽解释。谢谢

 firstNumber = (android.widget.EditText)findViewById(R.id.txtNumber1);
                secondNumber = (android.widget.EditText)findViewById(R.id.txtNumber2);
                addResult = (android.widget.TextView)findViewById(R.id.txtResult);
                btnAdd = (android.widget.Button)findViewById(R.id.btnAdd);   

代码:

包 app.com.myapplication;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;


public class MainActivity extends AppCompatActivity {

    //DECLERATION
    //declaring variables before the oncreation function
    android.widget.EditText firstNumber;
    android.widget.EditText secondNumber;
    android.widget.TextView addResult;
    android.widget.Button btnAdd;

    double num1,num2,sum;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);


        firstNumber = (android.widget.EditText)findViewById(R.id.txtNumber1);
        secondNumber = (android.widget.EditText)findViewById(R.id.txtNumber2);
        addResult = (android.widget.TextView)findViewById(R.id.txtResult);
        btnAdd = (android.widget.Button)findViewById(R.id.btnAdd);


        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }

        });



    }

    @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);
    }


}

XML

 <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="43dp"
        android:layout_marginLeft="20dp"
        android:text="Addition"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textSize="@dimen/abc_action_bar_default_height_material" />


    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="97dp"
        android:layout_below="@+id/textView1"
        android:text="Number One"
        android:textAppearance="?android:attr/textAppearanceMedium"



    <EditText
        android:id="@+id/txtNumber1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/textView2"
        android:layout_alignRight="@+id/textView1"
        android:ems="2"
        android:inputType="number"
        android:layout_alignParentEnd="false">

        <requestFocus />
    </EditText>

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView2"
        android:layout_below="@+id/textView2"
        android:layout_marginTop="47dp"
        android:text="Number Two"
        android:textAppearance="?android:attr/textAppearanceMedium" />


    <Button
        android:id="@+id/btnAdd"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView3"
        android:layout_alignRight="@+id/textView3"
        android:layout_below="@+id/textView3"
        android:layout_marginTop="46dp"
        android:text="Add" />

    <EditText
        android:id="@+id/txtNumber2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/btnAdd"
        android:layout_alignLeft="@+id/txtNumber1"
        android:ems="2"
        android:inputType="number" />

    <TextView
        android:id="@+id/txtResult"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/txtNumber2"
        android:layout_alignTop="@+id/btnAdd"
        android:textAppearance="?android:attr/textAppearanceMedium" />

根据您发布的代码,错误在这里:

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="97dp"
    android:layout_below="@+id/textView1"
    android:text="Number One"
    android:textAppearance="?android:attr/textAppearanceMedium"

这个街区没有关闭。您缺少“/>”。 应该是:

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="97dp"
    android:layout_below="@+id/textView1"
    android:text="Number One"
    android:textAppearance="?android:attr/textAppearanceMedium"/>

每当你得到那个编译时错误,你的 XML 中就有一个错误。这可能是复制和粘贴错误,但该错误仍然存​​在于您的资源中。