java - Invoking reflection APIs correctly - Android -


i trying invoke getselection method of webview. trying use reflection apis in android selected text.

i have extended webview (the new class named mywebview) add functionality. method getselection invoked within mywebview:

for(method m : webview.class.getdeclaredmethods()) {     if(m.getname().equalsignorecase("getselection")) {         m.setaccessible(true);         string str;         try {             log.v(this.tostring(), "is getselection available? " + m.getmodifiers() + " " + m.isaccessible());             str = (string) m.invoke(this, new object[] { null });             log.v(this.tostring(), "string selected = " + str);             toast.maketext(context, str, toast.length_long).show();   

mywebview non-activity class. running code results in following logcat output:

08-16 19:15:22.745: w/system.err(23452): java.lang.illegalargumentexception: object not instance of class 08-16 19:15:22.745: w/system.err(23452):    @ java.lang.reflect.method.invokenative(native method) 08-16 19:15:22.745: w/system.err(23452):    @ java.lang.reflect.method.invoke(method.java:507) 08-16 19:15:22.745: w/system.err(23452):    @ com.englishhelper.bluebottle.ehwebview$3.ontouch(ehwebview.java:210) 08-16 19:15:22.745: w/system.err(23452):    @ android.view.view.dispatchtouchevent(view.java:3934) 08-16 19:15:22.745: w/system.err(23452):    @ android.view.viewgroup.dispatchtouchevent(viewgroup.java:903) 08-16 19:15:22.745: w/system.err(23452):    @ android.view.viewgroup.dispatchtouchevent(viewgroup.java:942) 08-16 19:15:22.745: w/system.err(23452):    @ android.view.viewgroup.dispatchtouchevent(viewgroup.java:942) 08-16 19:15:22.745: w/system.err(23452):    @ android.view.viewgroup.dispatchtouchevent(viewgroup.java:942) 08-16 19:15:22.745: w/system.err(23452):    @ android.view.viewgroup.dispatchtouchevent(viewgroup.java:942) 08-16 19:15:22.745: w/system.err(23452):    @ com.android.internal.policy.impl.phonewindow$decorview.superdispatchtouchevent(phonewindow.java:1730) 08-16 19:15:22.745: w/system.err(23452):    @ com.android.internal.policy.impl.phonewindow.superdispatchtouchevent(phonewindow.java:1142) 08-16 19:15:22.745: w/system.err(23452):    @ android.app.activity.dispatchtouchevent(activity.java:2102) 08-16 19:15:22.745: w/system.err(23452):    @ com.android.internal.policy.impl.phonewindow$decorview.dispatchtouchevent(phonewindow.java:1714) 08-16 19:15:22.745: w/system.err(23452):    @ android.view.viewroot.deliverpointerevent(viewroot.java:2218) 08-16 19:15:22.745: w/system.err(23452):    @ android.view.viewroot.handlemessage(viewroot.java:1889) 08-16 19:15:22.745: w/system.err(23452):    @ android.os.handler.dispatchmessage(handler.java:99) 08-16 19:15:22.745: w/system.err(23452):    @ android.os.looper.loop(looper.java:123) 08-16 19:15:22.745: w/system.err(23452):    @ android.app.activitythread.main(activitythread.java:3691) 08-16 19:15:22.745: w/system.err(23452):    @ java.lang.reflect.method.invokenative(native method) 08-16 19:15:22.745: w/system.err(23452):    @ java.lang.reflect.method.invoke(method.java:507) 08-16 19:15:22.745: w/system.err(23452):    @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:847) 08-16 19:15:22.745: w/system.err(23452):    @ com.android.internal.os.zygoteinit.main(zygoteinit.java:605) 08-16 19:15:22.745: w/system.err(23452):    @ dalvik.system.nativestart.main(native method) 

my question is:
1. invoke method in reflection api requires pass object of class on intend call method on first argument. in case, object of class mywebview. how call invoke object of type mywebview?
2. same code works in case mywebview part of activity class.

the exception shows inside of anonymous inner class. problem accidentally passing instance of inner class instead of instance of ehwebview.

you can tell inside of inner class because after normal class name ehwebview, see $3, refers 3rd anonymous inner class of ehwebview. here relevant part of stack trace posed:

08-16 19:15:22.745: w/system.err(23452):    @ com.englishhelper.bluebottle.ehwebview$3.ontouch(ehwebview.java:210) 

typically, anonymous inner classes event handlers or runnables. appear , many people not aware using them.

you passing this invoke() method. inside of inner class, this refers instance of inner class instead of instance of outer class ehwebview.

to solve problem, pass reference of outer class removing this instead using ehwebview.this:

        str = (string) m.invoke(ehwebview.this, new object[] { null }); 

Comments

Popular posts from this blog

assembly - 8086 TASM: Illegal Indexing Mode -

Java, LWJGL, OpenGL 1.1, decoding BufferedImage to Bytebuffer and binding to OpenGL across classes -

javascript - addthis share facebook and google+ url -