meh
meh

Reputation: 59

Creating buttons in java

I'm pretty new to learning about java and android development; so please help me out. In the next line of code I am trying to create a button but I am having trouble understanding why the 'id' is underlined. When I fix it in the R folder the .blue_yes gets underlined and I am confused what I should do next? Any help would be helpful. Thanks

    Button yes = (Button) findViewById(R.id.blue_yes);
    yes.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            mBtAdapter.enable();
            setResult(CheckBluetoothEnabledActivity.RESULT_SUCCESS);
            finish();
        }
    });

    Button cancel = (Button) findViewById(R.id.blue_cancel);
    cancel.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            setResult(CheckBluetoothEnabledActivity.RESULT_CANCEL);
            finish();
        } 

Upvotes: 3

Views: 202

Answers (2)

fdreger
fdreger

Reputation: 12505

Given the data, one of gazillion possible explanations is that you don't have R imported (and then fix it by creating a new class, and the new class lacks the id attribute).

Upvotes: 0

codemaster
codemaster

Reputation: 572

you should have a button in the layout xml file with id blue_yes and blue_cancel before accessing that control in your in your code. probably you have not declared the control in your xml file correctly.

Once you specify a control in Layout file entry in R.Java is automatically made by eclipse android plugin. you don't need to mess with R folder and its file as R.java in automatically generated file and you should not change anything in that.

if even after declaring your control in layout file you are not able to get your control using findViewById method then please post your layout file as well to help you further.

Upvotes: 1

Related Questions