Anton Kim Anton Kim - 1 year ago 190
Android Question

setOnClickListener source - RuntimeException("Stub!")

I have this in onCreate:

mTrueButton = (Button) findViewById(;
mTrueButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {

This method is defined like this in Android API:

public void setOnClickListener(View.OnClickListener l) {
throw new RuntimeException("Stub!");

I understand that we create an anonymous class here to implement onClick() from View.OnClickListener interface.

However I am confused what does this method do exactly; it's not returning anything and not mutating anything, just throwing and exception so why do we have it here?


Answer Source

The source code you are looking at is the decompiled class from your SDK because you don't have the source of the target API set in your project.

To view the actual code:

  • Open your SDK manager
  • Download the Sources for Android SDK of the API version you have set as the target for your project.
  • Try to view the code again

Here is what I can see in my API 23 source:

 * Register a callback to be invoked when this view is clicked. If this view is not
 * clickable, it becomes clickable.
 * @param l The callback that will run
 * @see #setClickable(boolean)
public void setOnClickListener(@Nullable OnClickListener l) {
    if (!isClickable()) {
    getListenerInfo().mOnClickListener = l;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download