I'm just going to go ahead and say that my question is probably dumb. It's probably also been answered before, or is so obvious it doesn't need to be answered, but I just can't seem to find the right combination of words to find the appropriate solution.
I haven't programmed in a long time, so I might just be rusty but I'm really having a hard time getting this mistake figured out.
I'm writing an android app that uses a timer to count up how much money you're making. You enter an hourly wage, start the clock, and it's supposed to update every second showing how much you've made. However, I can't get the TextView
object in the timer task to be recognized, I get a missing symbol error from the compiler.
Here's the main activity where the user enters their wage.
public class IncomeCounter extends Activity
{
public final static String EXTRA_MESSAGE = "com.altotech.incomecounter.MESSAGE";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void sendMessage(View view){
Intent intent = new Intent(this, Clock.class);
EditText editText = (EditText) findViewById(R.id.edittextstring);
String enteredText = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, enteredText);
startActivity(intent);
}
}
Here's the second activity where the timer is supposed to start counting
public class Clock extends Activity{
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//get intent from IncomeCounter
Intent intent = getIntent();
Bundle bundle = new Bundle();
//create textview
TextView textview = new TextView(this);
textview.setTextSize(40);
setContentView(textview);
//initialize currentIncome, put in bundle
double currentIncome = 0;
bundle.putDouble("current", currentIncome);
//create and start timer
MyTimerTask myTask = new MyTimerTask();
Timer myTimer = new Timer();
myTimer.schedule(myTask, 0, 1000);
}
class MyTimerTask extends TimerTask{
public void run() {
runOnUiThread(new Runnable(){
public void run(){
//get intent and variables
Intent intent = getIntent();
Bundle bundle = intent.getExtras();
//scale incomeRate down to incomePerSecond, get currentIncome and add incomePerSecond to it, putDouble(currentincome), draw it onscreen, then repeat
String incomeRate = intent.getStringExtra(IncomeCounter.EXTRA_MESSAGE);
double incomePerSecond = (Double.parseDouble(incomeRate) / 3600);
double currentIncome = bundle.getDouble("current");
currentIncome = currentIncome + incomePerSecond;
bundle.putDouble("current",currentIncome);
textview.setText(Double.valueOf(currentIncome).toString());
}
});
}
}
}
The textview.setText(...);
line is where the issue's at. It's just, whenever I've looked at other examples of a timer being used with runinuithread
, they seemed to be able to setText
with no trouble at all. Maybe I'm misreading, or maybe I'm just dumb. But thanks for taking the time to read my question and sloppy code.
Try making textview
a member variable by declaring it outside of onCreate()
or any other method.
public class Clock extends Activity{
@SuppressLint("NewApi")
TextView textview; // declare it here
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//get intent from IncomeCounter
Intent intent = getIntent();
Bundle bundle = new Bundle();
//create textview
textview = new TextView(this); // and initialize it here