JuiCe
JuiCe

Reputation: 4201

Android Development : id cannot be resolved or is not a field

So I just started Android development, and I've already hit a wall. I have been looking around a whole lot online and on this website, but can't seem to find an answer that works for me. All I have written down so far in my Activity file is the following.

import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
public class CommanderActivity extends Activity
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) 
  {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.main);
     final Button button = (Button) findViewById( R.id.button_id );
  }    
}

It is in the last line of code ( R.id.button_id ) where the error arises. My XML file looks like this:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TableLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >
    <TableRow>
    <Button 
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:text="@string/Parameters"
        android:onClick="openParameters" />

    <Button
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:text="@string/Waves"
        android:onClick="openWaves" />
    </TableRow>
    <TableRow>
    <Button
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="@string/MoreParameters"
        android:onClick="openMoreParameters" />
    <Button
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="@string/Status"
        android:onClick="openStatus" />
    </TableRow>
    <TableRow>
    <Button
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="@string/Operations"
        android:onClick="openOperations" />

    <Button
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="@string/Monitor"
        android:onClick="openMonitor" />
    </TableRow>
    <TableRow>
    <Button
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="@string/CEBus"
        android:onClick="openCEBus" />
    <Button
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="@string/Advanced"
        android:onClick="openAdvanced" />
    </TableRow>
</TableLayout>
</LinearLayout>

Upvotes: 1

Views: 1285

Answers (2)

Proxy32
Proxy32

Reputation: 811

Just add the following line to your button android:id="@+id/my_button"
then within your activity you can use the method findViewById(R.id.my_button);

Hope this helps. I definitely recommend you reading this -> http://developer.android.com/guide/topics/ui/declaring-layout.html

Upvotes: 3

Gaurav Arora
Gaurav Arora

Reputation: 8362

You have not provided any id in your xml file. Please provide id in your xml file also then your error will be resolved.

Thanks

Upvotes: 2

Related Questions