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>
The
Rclass is code-generated for you by the Android build tools when you build your project. This means there are four possibilities:You have not tried building the project yet, in which case, try that.
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).
There is some bug in your manifest or one of your resources that is preventing
Rfrom being built. There should be Eclipse error indicators for this.You do not have an
android:idattribute in your layout, and therefore there is noR.idavailable.