nullpointerexception - Android findViewById returning null after changing compileSdkVersion and buildToolsVersion -
i've searched problem i'm having, specific , couldn't find answer it.
i have following code in app:
class mainactivity extends activity { ... @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); ... mstatusimageview = (imageview) findviewbyid(r.id.imageviewstatus); ... mstatusimageview.setontouchlistener(new view.ontouchlistener() { @override public boolean ontouch(view v, motionevent event) { ... } ... } i compiled , exported apk using:
compilesdkversion 21 buildtoolsversion "21.1.2" and works fine. updated app latest version:
compilesdkversion 23 buildtoolsversion "23.0.1" now here comes strange part. latest version in compilesdk , buildtools, if i'm in debug mode, runs fine.
but if export apk file, after installing in device, when run app following error:
java.lang.runtimeexception: unable start activity componentinfo{x.x.x/x.x.x.mainactivity}: java.lang.nullpointerexception: attempt invoke virtual method 'void android.widget.imageview.setontouchlistener(android.view.view$ontouchlistener)' on null object reference : @ x.x.x.mainactivity.oncreate(unknown source) this strange problem... guys have idea why happening?
you have call setcontentview() in oncreate() set ui before programmatically interacting of components. first 2 lines of oncreate() should super.oncreate(savedinstancestate), setcontentview(r.layout.your_layout). go ahead , set ontouchlistener.
Comments
Post a Comment