Im facing the following problem:
public class StatCounterService extends Service {
private TimerTask updateTask_ref = new TimerTask() {
//Get the element by Id as common practice in Android
TextView hungerValue_ref = (TextView)layout.findViewById(R.id.hungerValue);
@Override
public void run() {
//Every X Seconds, the Tamagotchi loses Hunger Points
int hunger = (int) (getHunger()-1);
//Updating Model (DataBase)
updateHunger(hunger);
//Now I want to update the TextView, but this hungerValue is Null
hungerValue_ref.setText(new Long(getHunger()).toString());
}
};
}
I think the problem is, Im looking for the ID of the View inside a Service, also inside a nested Class (the timertask). The Service is running, i just get a nullpointer on the “hungerValue”. Any Ideas how I could solve this?
thanks a lot!
Daniel
EDIT: I now try to update the view in the Timer from the activity, here is the idea:
public class MainWindowActivity extends Activity {
Timer timer_ref;
TextView hungerValue_ref;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Change the View I want to change initally (getting Value from Database)
updateHungerView();
//Start Timer
timer_ref = new Timer("StatCounterTimer");
timer_ref.schedule(updateTask_ref, 1000L, 10000L);
}
//The Timer from within I want to change the View every X seconds
private TimerTask updateTask_ref = new TimerTask() {
@Override
public void run() {
//Line Throws Exception, see Method below
updateHungerView();
}
};
/**
* Important Part
*/
public void updateHungerView() {
hungerValue_ref = (TextView) findViewById(R.id.hungerValue);
hungerValue_ref.setText(new Long(getHunger()).toString());
}
when I try to change the view from my activity, I get this:
09-30 11:54:48.967: ERROR/AndroidRuntime(30476): android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
To me it appears that you’re trying to find and update a TextView which might not be visible/inflated (e.g. it’s holding Activity has been destroyed/stopped/paused).
I’m guessing you want to use a service, because it can keep running even if your App gets pushed to the background or gets killed. I think however in your situation a change in the your design might be preferable.
Instead of having a Service running – save a timestamp as soon as your app gets paused (in the
onPause()-method of the visible activity). Then when the user resumes the App (theonResume-method will be called) you calculate the time between the previously saved timestamp and current time and decrease the hpvalue based on this. Then update the TextView from the holding activity.You could also consider using an AsyncTask – if the hpvalue only needs updating when the App is active.