java - Unable to initialize android view using custom annotation -


here i'm creating custom annotation bind view using resource id. annotation working in emulator not working in real devices.

bind.class

@retention(retentionpolicy.class) @target(elementtype.field) public @interface bind {  } 

binder.class

public class binder {     @suppresswarnings("trywithidenticalcatches")     public static void bind(appcompatactivity activity) {         for(field field : activity.getclass().getdeclaredfields()) {             field.setaccessible(true);             bind annotation = field.getannotation(bind.class);             if (annotation != null) {                 try {                     field id = r.id.class.getdeclaredfield(field.getname());                     field.set(activity, activity.findviewbyid(id.getint(id)));                 } catch (nosuchfieldexception e) {                     e.printstacktrace();                 } catch (illegalaccessexception e) {                     e.printstacktrace();                 }             }         }     } } 

sampleactivity.class

public class sampleactivity extends appcompatactivity {      @bind private toolbar toolbar;     @bind private button btnlogin;      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_login);         binder.bind(this);         setsupportactionbar(toolbar);         btnlogin.settext("login");     } } 

as run in device, binder unable initialize field , throwing following nullpointerexception

e/androidruntime: fatal exception: main e/androidruntime: java.lang.runtimeexception: unable start activity componentinfo{in.company.timesheet/in.company.timesheet.sampleactivity}: java.lang.nullpointerexception e/androidruntime:     @ android.app.activitythread.performlaunchactivity(activitythread.java:2309) e/androidruntime:     @ android.app.activitythread.handlelaunchactivity(activitythread.java:2366) e/androidruntime:     @ android.app.activitythread.access$600(activitythread.java:156) e/androidruntime:     @ android.app.activitythread$h.handlemessage(activitythread.java:1343) e/androidruntime:     @ android.os.handler.dispatchmessage(handler.java:99) e/androidruntime:     @ android.os.looper.loop(looper.java:153) e/androidruntime:     @ android.app.activitythread.main(activitythread.java:5330) e/androidruntime:     @ java.lang.reflect.method.invokenative(native method) e/androidruntime:     @ java.lang.reflect.method.invoke(method.java:511) e/androidruntime:     @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:841) e/androidruntime:     @ com.android.internal.os.zygoteinit.main(zygoteinit.java:608) e/androidruntime:     @ dalvik.system.nativestart.main(native method) e/androidruntime:  caused by: java.lang.nullpointerexception e/androidruntime:     @ in.company.timesheet.sampleactivity.oncreate(sampleactivity.java:20) e/androidruntime:     @ android.app.activity.performcreate(activity.java:5135) e/androidruntime:     @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1081) e/androidruntime:     @ android.app.activitythread.performlaunchactivity(activitythread.java:2273) e/androidruntime:     @ android.app.activitythread.handlelaunchactivity(activitythread.java:2366)  e/androidruntime:     @ android.app.activitythread.access$600(activitythread.java:156)  e/androidruntime:     @ android.app.activitythread$h.handlemessage(activitythread.java:1343)  e/androidruntime:     @ android.os.handler.dispatchmessage(handler.java:99)  e/androidruntime:     @ android.os.looper.loop(looper.java:153)  e/androidruntime:     @ android.app.activitythread.main(activitythread.java:5330)  e/androidruntime:     @ java.lang.reflect.method.invokenative(native method)  e/androidruntime:     @ java.lang.reflect.method.invoke(method.java:511)  e/androidruntime:     @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:841)  e/androidruntime:     @ com.android.internal.os.zygoteinit.main(zygoteinit.java:608)  e/androidruntime:     @ dalvik.system.nativestart.main(native method) 

  idea what's being missed me?


Comments

Popular posts from this blog

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -