java - How to change a background image? -


i studied possible answers , found should work:

int[] tabelazdjec={r.drawable.pic2,r.drawable.pic3}; linearlayout mylay=(linearlayout)findviewbyid(r.id.mylay); @override protected void oncreate(bundle savedinstancestate) {     // todo auto-generated method stub     super.oncreate(savedinstancestate);     setcontentview(r.layout.yess);     thread timer = new thread(){         public void run()         {             for(int i=0;i<tabelazdjec.length;i++)             {             try             {                  mylay.setbackgroundresource(tabelazdjec[i]);                 sleep(500);             }             catch(exception e)             {                 e.printstacktrace();             }          }}     };     timer.start(); 

it doesn't. error appears in second line declare linearlayout variable. if move declaration try{}catch{} part, app runs try{} doesn't execute, quite exact line. xml file goes follows:

<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:id="@+id/mylay" android:layout_height="match_parent" android:orientation="vertical" android:background="@drawable/pic1" >   </linearlayout> 

sorry if question seems trivial, i've spent last few hours staring @ line , can't find wrong it. maybe i'm blind...

you can't initialize view until have inflated layout. move linearlayout initialization below setcontentview()

linearlayout mylay;  @override protected void oncreate(bundle savedinstancestate) {     // todo auto-generated method stub     super.oncreate(savedinstancestate);     setcontentview(r.layout.yess);     mylay=(linearlayout)findviewbyid(r.id.mylay);     thread timer = new thread(){ 

your views return null if try initialize them before inflating layout belong setcontentview().

also, e.odebugg has pointed out in comment, trying update ui element in background thread next problem. need use runonuithread() update ui.


Comments

Popular posts from this blog

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

html - How to style widget with post count different than without post count -

url rewriting - How to redirect a http POST with urlrewritefilter -