Hello, I have a question on how to use a Handler.
Here the code in a nut shell
public class A extends Activity {
private Handler handler = new Handler();
private String foobar;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
......
Button button = (Button)this.findViewById(R.id.toggleButton);
button.setOnTouchListener(new ButtonHandler());
}
public void updateTextField() {
TextView aView = (TextView)this.findViewById(R.id.textLog);
aView.setText(foobar);
handler.postDelayed(mUpdateUITimerTask, 500);
}
private final Runnable mUpdateUITimerTask = new Runnable() {
public void run() {
// do whatever you want to change here, like:
updateTextField();
}
};
private class ButtonHandler implements OnTouchListener {
public ButtonHandler() {
}
public boolean onTouch(View v, MotionEvent event) {
handler.post(mUpdateUITimerTask);
return true;
}
}
}
When press the button I get
01-13 18:24:40.382: ERROR/AndroidRuntime(337): android.content.res.Resources$NotFoundException: String resource ID #0x0
I wonder if it has something to do that the listener is in an inner class.
Kind of a noob with the environment of Android.
I wrote this code in this editor it wasn't a direct copy and paste. So, there may be compilation errors in wrote I wrote.
Thank you for your suggestions.
dk
Here the code in a nut shell
public class A extends Activity {
private Handler handler = new Handler();
private String foobar;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
......
Button button = (Button)this.findViewById(R.id.toggleButton);
button.setOnTouchListener(new ButtonHandler());
}
public void updateTextField() {
TextView aView = (TextView)this.findViewById(R.id.textLog);
aView.setText(foobar);
handler.postDelayed(mUpdateUITimerTask, 500);
}
private final Runnable mUpdateUITimerTask = new Runnable() {
public void run() {
// do whatever you want to change here, like:
updateTextField();
}
};
private class ButtonHandler implements OnTouchListener {
public ButtonHandler() {
}
public boolean onTouch(View v, MotionEvent event) {
handler.post(mUpdateUITimerTask);
return true;
}
}
}
When press the button I get
01-13 18:24:40.382: ERROR/AndroidRuntime(337): android.content.res.Resources$NotFoundException: String resource ID #0x0
I wonder if it has something to do that the listener is in an inner class.
Kind of a noob with the environment of Android.
I wrote this code in this editor it wasn't a direct copy and paste. So, there may be compilation errors in wrote I wrote.
Thank you for your suggestions.
dk