Java redirect class method not found, needed for Android onClick from xml layout

343 views Asked by At

I am trying to find if it's possible to have a "method not found" 'catcher' on the Java objects, so that the method can be sent to another object which you know that has that method.

That is possible in other languages but I've never seen something similar on Java. I guess not... because you can't do something of the form myObject."methodName"(params); however, you can in Groovy which extends Java.

What I want to achieve, is to use the android:onClick property on the XML Layout in a custom View Object of which content is inflated.

The problem with that is that the onClick property searches the method on the parent Activity and not in the custom View, what causes the program to fail.

I would prefer to simply use the setOnClickListener(this) on my custom view, but for other reasons we are trying to avoid that, and is not very clean.

Do anyone knows an alternative?

Thanks in advance!

1

There are 1 answers

1
AlexR On BEST ANSWER

You are interesting in reflection that is supported in java since version 1.0. Yes, syntax like myObj."methodName"() is not supported byt you can say:

myObj.getClass().getMethod("myMethod").invoke(myObj).

For details take a look on reflection.