Jazzmine
Jazzmine

Reputation: 1875

Android Eclipse Adding button to an app

I am adding a new button to an existing Android app. Here's what I've done.

Added the XML the button to my main.xml file:

<Button
    android:id="@+id/btnNewButton"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="New Button"
/>

And added this to my activity:

Button btnNewButton = (Button) findViewById(R.id.btnNewButton);
btnNewButton.setOnClickListener(new View.OnClickListener() {
    public void onClick (View v) {
        MyClass.getValue("page: btnNewButton");
    }
});

There is definitely an error in my Activity and it offers to Change to btnNewButton. When I accepted the suggestion, I noticed it added a line in R.java:

public static int btnNewButton;

Also, this is underlined in red findViewById(R.id.btnNewButton); and I'm getting this error when I hover over the red X: btnNewButton cannot be resolved or is not a field.

I think there is another file I'm supposed to add information about the button but I'm not sure which one it is.

Can someone help?

Thanks

Resolution - I cleaned my project and removed the R.java. One or both of which resolved the problem.

Upvotes: 0

Views: 2044

Answers (1)

orip
orip

Reputation: 75427

Clean and re-build, possibly restart Eclipse as well. It seems like your R.java file wasn't regenerated after you added the button to the XML layout.

Another option is that your layout isn't in the layouts path: is this where you put all your layouts?

Upvotes: 2

Related Questions