Why does a protected android:onClick method in Activity actually work?

只谈情不闲聊 提交于 2019-12-31 09:09:10

问题


Suppose you define android:onClick="doClick" in your Activity as

protected void doClick(View view) { }

The documentation states that

This name must correspond to a public method that takes exactly one parameter of type View.

This is a given requirement of the underlying Class.getMethod() method, which only finds public methods as the documentation states that it

Returns a Method object that reflects the specified public member method of the class or interface represented by this Class object.

So how is it possible, that this implementation, which should not work at all, works on some devices and emulators, while it doesn't work on others using the same API levels as well?


回答1:


As per "The Java™ Tutorials" : The protected modifier specifies that the member can only be accessed within its own package (as with package-private) and, in addition, by a subclass of its class in another package




回答2:


I debugged the particular implementations. The relevant portion of the code is within the Support Library using Class.getMethod().

As stated in the documentation, this method only finds public member methods and behaves correctly. For some reason, all modifiers of declared protected methods of the Activity (these are onCreate() and doClick()) are set to 1, which means these are actually public.

I could only observe this behavior creating the debug build with a Mac. So why this happens is still an open question, which I'm trying to find an answer to.



来源:https://stackoverflow.com/questions/50593014/why-does-a-protected-androidonclick-method-in-activity-actually-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!