user379888
user379888

Reputation:

Id is not resolved or is not a field

I am developing my first program using Android. It adds or subtracts to a total depending on which button the user clicks. I have made its xml and now I was creating its java but I getting some errors(Its not fully developed yet). I would be helpful if anyone can guide me.

main.xml

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

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Your Total is 0" 
        android:gravity="center"    

        />

  <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="+1"
        android:layout_gravity="center" 
        android:text="Button" />  


    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="-1" 
        android:layout_gravity="center" 
        android:text="Button" />



</LinearLayout>

Java Source File

package com.fahad.project;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class AndroidProject1Activity extends Activity {
    /** Called when the activity is first created. */
    int numberCount;
    Button add, sub;
    TextView display;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        numberCount =0;
        add=(Button)findViewById(R.id.bAdd);<!--error-->
        sub=(Button)findViewById(R.id.bSub);<!--error-->
        display=(TextView)findViewById(R.id.tvDisplay);<!--error-->

        add.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub

            }
        });

        sub.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub

            }
        });


    }
}

Error

Id is not resolved or is not a field

Please help me out with this error. Thanks.

EDIT

Pasting its .R file,

/* AUTO-GENERATED FILE.  DO NOT MODIFY.
 *
 * This class was automatically generated by the
 * aapt tool from the resource data it found.  It
 * should not be modified by hand.
 */

package com.fahad.project;

public final class R {
    public static final class attr {
    }
    public static final class drawable {
        public static final int ic_launcher=0x7f020000;
    }
    public static final class layout {
        public static final int main=0x7f030000;
    }
    public static final class string {
        public static final int app_name=0x7f040001;
        public static final int hello=0x7f040000;
    }
}

Upvotes: 1

Views: 1427

Answers (3)

Anton
Anton

Reputation: 4403

You declared buttons ids as "button1" and "button2" but refer to them as "bAdd" and "bSub" which won't work.

If your code doesn't compile even when you corrected the references, do the Right Click on the project -> Refresh and Project->Clean in Eclipse.

If it still doesn't work then you have probably messed up the project setup. Since you are obviously doing some first steps only, just create a new project and copy your code there.

Upvotes: 1

Adil Soomro
Adil Soomro

Reputation: 37729

Ok! which id do you put in xml layout is generated automatically in R.id class.

for example:

<Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="+1"
        android:layout_gravity="center"
        android:text="Button" /> 

in this xml Button tag you put android:id="@+id/button1" and id as button1 is automatically added to R.id

And you connect your Button in java code using this id not your own

like this

add=(Button)findViewById(R.id.button1);

Upvotes: 1

Will Tate
Will Tate

Reputation: 33509

Use the ID values from your XML layout. Try...

add=(Button)findViewById(R.id.button1);
sub=(Button)findViewById(R.id.button2);

You may want to give them more descriptive IDs moving forward!

You haven't given the TextView an ID in your XML, I will leave that one for you to fix :]

Upvotes: 1

Related Questions