setOnClickListener源-RuntimeException(“ Stub!”) [英] setOnClickListener source - RuntimeException("Stub!")

查看:99
本文介绍了setOnClickListener源-RuntimeException(“ Stub!”)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在onCreate中拥有这个:

I have this in onCreate:

    mTrueButton = (Button) findViewById(R.id.true_button);
    mTrueButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            checkAnswer(true);
        }
    });

此方法在Android API中的定义如下:

This method is defined like this in Android API:

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

我知道我们在这里创建了一个匿名类来实现View的onClick() .OnClickListener界面。

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?

谢谢。

推荐答案

您正在查看的源代码是SDK中的反编译类,因为您没有在项目中设置目标API的源。

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.

要查看实际代码:


  • 打开您的SDK管理器

  • 下载已设置为项目目标的API版本的 Android SDK的来源

  • 尝试再次查看代码

这是我在API 23来源中看到的内容:

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()) {
        setClickable(true);
    }
    getListenerInfo().mOnClickListener = l;
}

这篇关于setOnClickListener源-RuntimeException(“ Stub!”)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆