random - Randomly set background image - Android -
random - Randomly set background image - Android -
i have background set on login screen, , want randomly set background every time app loaded pile of images in drawables folder. however, app crashes every effort @ writing code this.
i have array defined in layout/strings.xml:
<?xml version="1.0" encoding="utf-8"?>
<string name="app_name">myfsu</string> <string name="hello_world">hello world!</string> <string name="action_settings">settings</string> <style name="defaultbuttontext"> <item name="android:layout_width">fill_parent</item> <item name="android:layout_height">wrap_content</item> <item name="android:textcolor">#979797</item> <item name="android:gravity">center</item> <item name="android:layout_margin">3dp</item> <item name="android:textstyle">normal</item> <item name="android:textsize">20sp</item> <item name="android:shadowcolor">#f9f9f9</item> <item name="android:shadowdx">1</item> <item name="android:shadowdy">1</item> <item name="android:shadowradius">1</item> </style> <array name="myimages"> <item>@drawable/a</item> <item>@drawable/b</item> <item>@drawable/c</item> <item>@drawable/d</item> <item>@drawable/e</item> </array>
and here's code generate random image:
public class mainactivity extends actionbaractivity { final relativelayout background = (relativelayout) findviewbyid(r.id.back); resources res = getresources(); final typedarray myimages = res.obtaintypedarray(r.array.myimages); final random random = new random(); public void backgrounds(view v) { int randomint = random.nextint(myimages.length()); int drawableid = myimages.getresourceid(randomint, -1); background.setbackgroundresource(drawableid); }
logcat showing null pointer exception @ <init>
, have no clue or how prepare it. :(
does have suggestions?
you should move
final relativelayout background = (relativelayout) findviewbyid(r.id.back); resources res = getresources(); final typedarray myimages = res.obtaintypedarray(r.array.myimages);
inside oncreate
, after setcontentview
. setcontentview(int), inflate , place inflated view within activity
view's hierarchy, allowing r.id.back
, in case, within it
android random
Comments
Post a Comment