Android Studio and the Case of the Rogue Parentheses

TL;DR: Follow bug report instructions in the order they are presented, as order may impact your ability to reproduce the bug.

Android Studio 2.2.x has a bug (also reported here and here). If you do things in a specific sequence, you will wind up in a state where the Properties pane of the GUI editor will generate buggy layout XML.

The catch is that the sequence of events matters.

When I first tried to reproduce the bug, I did this:

  • Created a scrap project

  • Dragged a Button into my activity’s layout

  • Added the method to my activity that I wanted to use with android:onClick

  • Attempted to set the android:onClick attribute via the drop-down list in the Properties pane

This sequence illustrates a different bug — the drop-down does not include my method — but it does not reproduce the specific reported issue.

This sequence of events, though, reproduces it nicely:

  • Created a scrap project

  • Added the method to my activity that I wanted to use with android:onClick

  • Dragged a Button into my activity’s layout

  • Attempted to set the android:onClick attribute via the drop-down list in the Properties pane

There, the drop-down list will contain the activity name in parentheses, after the method name. So, if your activity is MainActivity and your method is foo(), the drop-down list has foo (MainActivity). If you choose it, you wind up with android:onClick="foo (MainActivity)". That is invalid syntax, as android:onClick only takes the name of the method. As a result, you crash when clicking the Button, because Android cannot find a method with the name foo (MainActivity), in part because that is not a legal method name.

Fortunately, Ravikumar N managed to set me straight.

Ideally, Lint would have caught this, on the ground that android:onClick should only have something that could be a valid Java method name (or a data binding expression). I filed a feature request for that Lint check.

But, the moral of this story is: follow bug-reproduction instructions to the letter, as best you can. In my case, I assumed that the order of creating the Button and adding the method did not matter, and that was an invalid assumption.