tags:

views:

60

answers:

4

I am trying to create a simple button example, but when I add this code:

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

it wont update my R.id file. I've tried everything including making sure automatic build is on, cleaning the project, and updating the SDK. This happens in both 1.6 and 2.2 projects.

Here's what full code is looking like:

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

public class NewTest extends Activity {

 Button myButton;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        myButton = (Button) findViewById(R.id.button1);
    }
}
A: 

Since you are using XML based Layout make sure that you have android:id attribute in the main.xml file with "@+id/button1" as its value.......

A: 

findViewById is looking at the R.id file for the location you refer to.

Your code will not cause the file to update as it is only looking for the button.

Creating the button in your layout will cause the R.id file to update.

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

As you are using findViewById in context, and the view for the current context is main.xml, the above button example will need to be inside the main.xml file. Otherwise if you had another button with the same name in another .xml layout, the entry would be made in your R.id file but your code would give you a null pointer exception, because the button doesn't exist in the context you are trying to reference it from.

Scoobler
A: 

It wouldn't update the generated R.java file when you just refer to an existing id.

It only updates when you add a resource. button01 must already exist in one of your .xml files, otherwise your 'findViewById(R.id.button1)' wouldn't compile.

NickT
A: 

Make sure you have proper import for your app's R class in your Activity:

import your.app.package.R;

UPDATE: this implicit import is only needed if your Activity class is not in the root of your.app.package package.

Arhimed
That's not how it works. You can't import R like this or it won't work at all.
Falmarri
Well, if I comment out the R import, then my `(Button) findViewById(R.id.acceptBtnTop)` stops working. So I am really sure without the import of the auto-generated R class the above code will not work. Since the code in the main post does not contain R import I suggested to check it.
Arhimed
Oh my mistake. I was thinking you were doing `import android.R`. Because THAT won't work. As for the above, it's unnecessary. You don't need to import classes in your package. I can't change my downvote unless you edit the question
Falmarri
Ok, I fixed/updated my answer.
Arhimed
@Falmarri: He's edited the answer now, so you can change your vote. (@Arhimed: Use "@user" if you need to get someone's attention in a comment.)
Michael Myers