0

In an android Activity class, I saw the bridging to a Button of XML file and the setting it for click listener was using findViewById():

public class MyClass1 extends Activity implements OnClickListener {

    Button b;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.photo);

        b = (Button) findViewById(R.id.button1);    //This is where I have question
        b.setOnClickListener(this);
    }

    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.button1:

            break;
        case R.id.button2:

            break;
        }
    }
}

But As for referencing a RadioGroup (In another Activity class), It wasn't pointed to the Object by the findViewById() as it was for the Button:

public class MyClass2 extends Activity implements OnCheckedChangeListener {

    RadioGroup rg;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.photo);

        //Why isn't this here? --> rg = (RadioGroup) findViewById(R.id.radiogroup);
        rg.setOnCheckedChangeListener(this);
    }

   @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        // TODO Auto-generated method stub
        switch (checkedId) {
        case R.id.rBad:

            break;
        case R.id.rGood:

            break;
        }
    }
}

I mean in both onClick() and onCheckedChanged() methods the id of objects is referenced to.

So why b = (Button) findViewById(R.id.button1); is declared in the first code snippt, while the rg = (RadioGroup) findViewById(R.id.radiogroup); isn't in the second snippet.

Is it something related to RadioGroup or it's applicable to other objects too?

Pankaj
  • 7,908
  • 6
  • 42
  • 65
DummyBeginner
  • 411
  • 10
  • 34
  • Doesn't the RadioGroup example create a NullPointerException, since the rg is never assigned? – Finnboy11 Jun 06 '15 at 13:27
  • 2
    The second code snippet appears to have a bug - it definitely _should_ have the `findViewById` call. Note there are view injection libraries such as [ButterKnife](http://jakewharton.github.io/butterknife/) which avoid the need for this, but your example here doesn't include any of the code required for that. – Adam S Jun 06 '15 at 13:35
  • @Finnboy11 I just see this code in a tutorial and didn't run it. – DummyBeginner Jun 06 '15 at 13:50
  • Thanks @AdamS for introducing `ButterKnife`. – DummyBeginner Jun 06 '15 at 13:50
  • @DummyBeginner please reference the tutorial. It seems that the tutorial itself had a bug in it. – Finnboy11 Jun 06 '15 at 13:51
  • @Finnboy11 It was an episode of a series [here](https://www.thenewboston.com/videos.php?cat=6&video=16767), But I'm positive since there was a bug like this, the instructor definitely corrected that in next episodes. – DummyBeginner Jun 06 '15 at 13:57
  • @DummyBeginner Wow, that tutorial is really bad. The autor doesn't run his code regularely and thus small issues like that get to the code and stay there for several episodes. It's one of the reasons why I personally don't suggest to learn coding from video tutorials. They are usually recorded live with the chance of issues, they are hard to reference when issues appear and it's hard to follow them while trying to write your own code at the same time. Plus they can't be copy-pasted for shameless tutorial stealing (which is actually really effective learning method, at least to my mind) :P – Finnboy11 Jun 06 '15 at 14:02
  • @Finnboy11 Ha ha. About future referencing, issues due to live recording, writing the code at the same time I agree with you. But let me defense the instructor of this tutorial, he actually recommended to run the code regularly, several times in prior episodes, But this time he missed it. You know accidents happen :P Also, he has the sense of humor through the tutorial, that make it more appealing to me. – DummyBeginner Jun 06 '15 at 18:08

1 Answers1

2

The RadioGroup example shouldn't even run, because the rg variable is never assigned and thus creates a NullPointerException when you try to set the onCheckedChanged listener. Essentially there should be no differences with the usage of the Button and RadioGroup classes, at least in this regard. This is more a pure Java thing than an Android specific subject.

What you may have possibly stumbled upon is defining the listeners in the XML code, as in this StackOverflow answer. However, this doesn't directly work for the onCheckedChanged so it's weird that the situation here is this way around...

Edit:

Quoted from Adam S: Note there are view injection libraries such as ButterKnife which avoid the need for this, but your example here doesn't include any of the code required for that.

However, it seems that the tutorial from which the code was taken had a bug in it.

Community
  • 1
  • 1
Finnboy11
  • 986
  • 2
  • 15
  • 34