简体   繁体   中英

Beginner Android why is “id” is “Unknown member” with R.id?

I'm sure this is something dead simple as it's my first android app with code (the hello world example I did was just assigning a value to a string in XML). My problem is when trying to get the reference for my button in my variable then the id as in R.id is not defined?

The compiler error is in a comment in the code below where it occurs:

package com.geeksonhugs.simplecalc;

import android.app.*;
import android.os.*;
import android.view.*;
import android.widget.*;

public class MainActivity extends Activity
{
    private Button AddButton;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        AddButton = (Button)findViewById(R.id.btnAdd);
//"Unknown member 'id' of 'com.geeksonhugs.simplecalc.R'"

        //AddButton.setOnClickListener(this);

    }   

}

XML layout file:

<?xml version='1.0' encoding='utf-8' ?>
<LinearLayout 
        xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/LayoutRoot" 
    android:layout_height="match_parent" 
    android:layout_width="match_parent" 
    android:orientation="vertical">

<TextView 
    android:id="@+id/txtFirstNum" 
    android:layout_height="wrap_content" 
    android:layout_width="match_parent" 
    android:text="@string/strFirstNum" />

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

<TextView 
    android:id="@+id/txtSecondNum" 
    android:layout_height="wrap_content" 
    android:layout_width="match_parent" 
    android:text="@string/strSecondNum" />

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

<Button 
    android:id="@+id/btnAdd" 
    android:layout_height="wrap_content" 
    android:layout_width="match_parent" 
    android:text="@string/strAdd" 
    android:gravity="center" />

<TextView 
    android:id="@+id/txtResult" 
    android:layout_height="wrap_content" 
    android:layout_width="match_parent" 
    android:text="" />

</LinearLayout>

我通过删除bin文件夹并使用AIDE App重新编译解决了该问题。

The R class is code-generated for you by the Android build tools when you build your project. This means there are four possibilities:

  1. You have not tried building the project yet, in which case, try that.

  2. Eclipse doesn't think it needs to build the project for some reason -- try Project > Clean from the Eclipse menu (only relevant if you are using Eclipse, of course).

  3. There is some bug in your manifest or one of your resources that is preventing R from being built. There should be Eclipse error indicators for this.

  4. You do not have an android:id attribute in your layout, and therefore there is no R.id available.

Please make sure the xml file you pasted is called main.xml and under layout folder. And try to generate the R file again. That may help.

For those using AIDE having issues referencing Id, try deleting the "gen folder" located in the bin folder and recompile. The problem is the R.java class did not create a constructor called Id. So deleting the gen folder containing the R.java class and recompiling solves this issue.

You must add id to a component in the XML file. Like this:

android:id="@+id/buton1"

Now can you click mainActivity.java and write code "R.id." you a see hint to write "buton1" and no error of "id"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM