Newbie confused about Android findViewById () functions?

I am very new when it comes to Android development, so bear with me. I am currently using Windows 8 / Eclipse.

My problem is that the function findViewById

seems to have gone crazy, although I could very well have been using it incorrectly.

The app worked fine, then I "dropped" a few new ones TextView

on the page that seem to completely confuse it.

The function findViewById

now either finds the wrong control entirely, or finds nothing at all (and returns null). I checked my file activity_main.xml

and the ID is still correct.

Can anyone please help?

This is a typical example of my use:

public void toggleButtonNetwork_Click(View v) {
  ToggleButton tb = (ToggleButton) this.findViewById(R.id.toggleButtonNetwork);//did work, now does not work!
}

      

The only understanding I can add is that my R.java file looked like this when it worked:

...
    public static final class id {
      public static final int menu_settings=0x7f070004;
      public static final int textViewGPS=0x7f070003;
      public static final int textViewNetwork=0x7f070001;
      public static final int toggleButtonGPS=0x7f070002;
      public static final int toggleButtonNetwork=0x7f070000;
    }
...

      

and now looks like this:

public static final class id {
    public static final int menu_settings=0x7f070006;
    public static final int textView1=0x7f070004;
    public static final int textView2=0x7f070005;
    public static final int textViewGPS=0x7f070002;
    public static final int textViewNetwork=0x7f070003;
    public static final int toggleButtonGPS=0x7f070000;
    public static final int toggleButtonNetwork=0x7f070001;
}

      

+3


source to share


3 answers


This is a fairly common problem. Try and run Project/Clean...

on your project. Sometimes it happens that the automatic generation of classes R

goes wrong and this will force them to rebuild.



+5


source


Everything is good.



Always just go to project -> clean AND than run . Here it is.

+3


source


This does not answer your question. I suspect the comment A-Cs is correct.

But there is nothing to be confused. findViewById

really simple.

When you compile your application, the compiler creates R.java and adds a string for each view in your XML layouts (and strings, drawings, colors, and so on - whatever is a "resource") and gives it a unique identifier.

public static final int toggleButtonNetwork=0x7f070001;

      

The ID will change when your resources change, but that doesn't matter. When you use findViewById

, you give it a "friendly name", in this case R.id.toggleButtonNetwork

, which is compiled to 0x7f070001

because it public static final int toggleButtonNetwork

is a static constant.

When you inflate a view from XML, typically with setContentView, an object view hierarchy is created. The identifier for each object is the identifier found in R.java.

findViewById

returns a reference to an object of type View

, which you then cast onto any View, so you use

= (Button)findViewById(R.id.toggleButtonNetwork);

      

+1


source