I’m learning android, and I got into a little problem.
I’ve got a button I want to move to the right side of the screen, after i click on it.
I added a timer that starts after the button gets clicked, but when I click on the button the program/activity crashes.
here is the code:
import android.app.Activity;
import android.graphics.Point;
import android.os.Bundle;
import android.view.Display;
import android.view.View;
import android.widget.Button;
public class Testing extends Activity{
Timer timer = new Timer();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.testing_layout);
final Button bt = (Button)findViewById(R.id.testButton);
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
timer.scheduleAtFixedRate(new TimerTask(){
@Override
public void run() {
float btLoc = bt.getX();
bt.setX(btLoc+= 50);
}
}, 2000, 2000);
}
});
}
this same piece of code that moves the button worked before without the timer. I have no idea why the program crashes when the onClick(View v) executes.
(sorry for my english)
Use runOnUiThread for Updating UI from non UI Thread :