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

Popular posts from this blog

1111. appearing after print sequence - php -

java - WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/board/] in DispatcherServlet with name 'appServlet' -

Ruby on Rails, ActiveRecord, Postgres, UTF-8 and ASCII-8BIT encodings -