java - Updating each second in Android -
i making app counts every second based on rate of pay, , written now, crashes on startup.
am doing thread section wrong?
i kind of new android, bit unclear on oncreate method in general. clarification , how relates code helpful well.
the button supposed start count. think it's crashing due t.start() line, don't know how trigger event.
package com.example.terik.myapplication; import android.os.bundle; import android.support.v7.app.actionbaractivity; import android.view.menu; import android.view.menuitem; import android.widget.textview; public class mainactivity extends actionbaractivity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); textview text2; text2 = (textview) findviewbyid(r.id.textview2); thread t = new thread() { @override public void run() { try { while (!isinterrupted()) { thread.sleep(1000); runonuithread(new runnable() { @override public void run() { updatetextview(); } }); } } catch (interruptedexception e) { } } }; t.start(); } private void updatetextview() { textview text2; double update; double rateofpay = 9.00; text2 = (textview)findviewbyid(r.id.textview2); charsequence newtime = text2.gettext(); int number = integer.parseint(newtime.tostring()); update = number+ rateofpay; text2.settext((int) update); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.menu_main, 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); } }
one problem numberformatexception when trying parse double integer.
the other problem trying call settext()
int on line:
text2.settext((int) update);
this fixed it:
private void updatetextview() { textview text2; double update; double rateofpay = 9.00; text2 = (textview)findviewbyid(r.id.textview2); charsequence newtime = text2.gettext(); double number = double.parsedouble(newtime.tostring()); update = number+ rateofpay; text2.settext(string.valueof(update)); }
edit:
here's how start thread when click button. first make thread t
instance variable, can accessed in button click run()
method (you might want re-name method too!).
i tested this, worked me:
public class mainactivity extends actionbaractivity { thread t; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); textview text2; text2 = (textview) findviewbyid(r.id.textview2); t = new thread() { @override public void run() { try { while (!isinterrupted()) { thread.sleep(1000); runonuithread(new runnable() { @override public void run() { updatetextview(); } }); } } catch (interruptedexception e) { } } }; } public void run(view v) { t.start(); } private void updatetextview() { textview text2; double update; double rateofpay = 9.00; text2 = (textview)findviewbyid(r.id.textview2); charsequence newtime = text2.gettext(); double number = double.parsedouble(newtime.tostring()); update = number+ rateofpay; text2.settext(string.valueof(update)); } //.........
edit 2:
as @bladecoder mentioned in comments, thread over-kill this. using handler , postdelayed() best route this.
also, better make textview instance variable don't create new reference every time update it.
i tested version well:
public class mainactivity extends actionbaractivity { textview text2; handler handler; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); text2 = (textview) findviewbyid(r.id.textview2); handler = new handler(); } runnable updatetext = new runnable(){ @override public void run(){ updatetextview(); handler.postdelayed(this, 1000); } }; public void run(view v) { handler.postdelayed(updatetext, 1000); } private void updatetextview() { double update; double rateofpay = 9.00; charsequence newtime = text2.gettext(); double number = double.parsedouble(newtime.tostring()); update = number+ rateofpay; text2.settext(string.valueof(update)); } //.............
Comments
Post a Comment