不能解决R.Id

时间:2015-11-07 02:20:29

标签: android 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);   

CODE:

包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" />

1 个答案:

答案 0 :(得分:1)

根据您发布的代码,错误在此处:

<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"

此区块未关闭。你缺少“/&gt;”。 应该是:

<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就会出错。这可能是复制和粘贴错误,但错误仍然存​​在于您的资源中。