0

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.

asked Dec 1, 2013 at 21:19
4
  • What is the error that you are getting?Seems to be fine from the code that you pasted Commented Dec 1, 2013 at 21:22
  • So what's the problem? And for buttons, all you need to add it to is the Activity and the XML layout, nothing else. It's added to R automatically. Commented Dec 1, 2013 at 21:22
  • Can't see anything wrong with how you added the button. Your call to MyClass.getValue() seems a bit odd though. Why all that blank space before the String "page: btnNewButton"? Commented Dec 1, 2013 at 21:48
  • I didn't see this earlier but you all are right. This is underlined in red findViewById(R.id.btnNewButton); and I'm getting this error when I hover over the red X in the activity: btnNewButton cannot be resolved or is not a field. I hope this clarifies what's wrong. I'll add this above in my original question as it is important information. Thanks everyone. Commented Dec 1, 2013 at 21:54

1 Answer 1

2

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?

answered Dec 1, 2013 at 22:01
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the good tip, I guess I need to clean up my project and delete R.java if I run into this problem again.
+1 This could happen if he had errors in XML - it forces R.java to recreate again.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.