android - Looping in onCreate method -
so i'm creating program randomly spawn circles on screen every 5 seconds. had working displaying 1 circle when tried loop in oncreate method draw multiple circles, got compiler errors , have no idea now, been stuck 30 minutes. here code , main error. appreciated.
public class randomcircles extends actionbaractivity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); timer timer = new timer(); timer.scheduleatfixedrate(new timertask() { public void run() { setcontentview(new myview(r.layout.activity_random_circles); } }, 0, 5 * 1000l);//5 seconds } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.menu_random_circles, menu); return true; } @override public boolean onoptionsitemselected(menuitem item) { // handle action bar item clicks here. action bar // automatically handle clicks on home/up button, long // specify parent activity in androidmanifest.xml. int id = item.getitemid(); //noinspection simplifiableifstatement if (id == r.id.action_settings) { return true; } return super.onoptionsitemselected(item); } } class myview extends view { public myview(context context) { super(context); // todo auto-generated constructor stub } @override protected void ondraw(canvas canvas) { super.ondraw(canvas); //800, 1162 random rand = new random(); double x = rand.nextint(getwidth()); if(getwidth() - x < 100) x -= 100; else if(getwidth() - x > getwidth() - 100) x += 100; double y = rand.nextint(getheight()); if(getheight() - y < 100) y -= 100; else if(getheight() - x > getheight() - 100) y += 100; int radius; radius = 100; paint paint = new paint(); paint.setstyle(paint.style.fill); paint.setcolor(color.white); canvas.drawpaint(paint); // use color.parsecolor define html colors paint.setcolor(color.parsecolor("#cd5c5c")); canvas.drawcircle(x, y, radius, paint); } }
your myview
constructor seeks context
parameter, passing r.layout.activity_random_circles
auto-generated int value mapped layout xml file in res/layout
. try passing randomcircles.this
instead refers activity
context
Comments
Post a Comment