Sunday, November 15, 2015

Some functions work only in some contexts - example of Java / Android findViewById()

Calling findViewById() on the Activity object will only work if the current Activity layout is set by setContentView. If you add a layout through some other means, then you need the View object of the layout and call findViewById() on it.

View v = inflater.inflate(id_number_of_layout);
View innerView = v.findViewById(id_number_of_view_inside_v);


Original thread:

http://stackoverflow.com/questions/9228777/findviewbyid-not-working-in-a-not-mainactivity-class

If you go with Unit Testing or TDD approach, and split the problem into tiny subproblems, then you are more likely to find a solution quickly. In this case, it was a matter of context in which findViewById() was called.

No comments:

Post a Comment

Ubuntu 12.04, 14.04, 16.04 - auto start an app or script before login

To run a command or application at startup, even before the user has logged in, you can use this file: /etc/rc.local The commands entered...