Android baseactivity $1. onclick anonymous internal class this. getclass () Problem

Source: Internet
Author: User

 
I want to write a component. I can use the annotation method to automatically find this view and find the method for clicking events, like this (the annotation class will not be posted ):
@ Viewclick (viewid = R. Id. clickbtn, clickmethod = "btnclickmethod ")
Private button btnclick;
However, some problems are encountered in the process. The log is as follows:
 
12-27 19:04:51. 090: W/system. err (14345): Java. lang. illegalargumentexception: Expected extends er of Type COM. mobile. component. setting. syssettingactivity, but got Java. lang. class <COM. mobile. component. setting. syssettingactivity>
12-27 19:04:51. 100: W/system. Err (14345): At java. Lang. Reflect. method. invokenative (native method)
12-27 19:04:51. 100: W/system. Err (14345): At java. Lang. Reflect. method. Invoke (method. Java: 511)
12-27 19:04:51. 100: W/system. Err (14345): At com. Mobile. component. Base. baseactivity $1. onclick (baseactivity. Java: 80)
12-27 19:04:51. 100: W/system. Err (14345): at Android. View. View. Adjust mclick (view. Java: 3538)
12-27 19:04:51. 100: W/system. Err (14345): at Android. View. View $ invoke mclick. Run (view. Java: 14319)
12-27 19:04:51. 100: W/system. Err (14345): at Android. OS. handler. handlecallback (handler. Java: 608)
12-27 19:04:51. 100: W/system. Err (14345): at Android. OS. handler. dispatchmessage (handler. Java: 92)
12-27 19:04:51. 100: W/system. Err (14345): at Android. OS. low.loop (low.java: 156)
12-27 19:04:51. 100: W/system. Err (14345): at Android. App. activitythread. Main (activitythread. Java: 5045)
12-27 19:04:51. 100: W/system. Err (14345): At java. Lang. Reflect. method. invokenative (native method)
12-27 19:04:51. 100: W/system. Err (14345): At java. Lang. Reflect. method. Invoke (method. Java: 511)
12-27 19:04:51. 100: W/system. Err (14345): At com. Android. Internal. OS. zygoteinit $ methodandargscaller. Run (zygoteinit. Java: 784)
12-27 19:04:51. 100: W/system. Err (14345): At com. Android. Internal. OS. zygoteinit. Main (zygoteinit. Java: 551)
12-27 19:04:51. 100: W/system. Err (14345): At Dalvik. system. nativestart. Main (native method)

Analysis: I set the Click Event and view in a class method. setonclicklistener (), so COM. mobile. component. base. baseactivity $1. onclick (baseactivity. java: 80) This line of log, baseactivity $1. onclick indicates an anonymous internal class, such as onclick;
Call this in The onclick method. getclass () is not the current class, but an anonymous internal class, similar to baseactivity $1. onclick, so I always prompt that I cannot find the method of my class, so I will do this: baseactivity. this. getclass (). After the transformation, the current class is obtained.

View. setonclicklistener (New onclicklistener (){
@ Override
Public void onclick (view v ){
Method method;
Log. I ("tag", "baseactivity. this = "+ baseactivity. this + "\ n" + "baseactivity. this. getclass () = "+ baseactivity. this. getclass ());
Method = baseactivity. This. getclass (). getdeclaredmethod (clickmethod, view. Class );
If (method! = NULL ){
Method. Invoke (baseactivity. This, V );
} Else {
Throw new illegalaccessexception ("No such method ");
}
}
}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.