Can't access resources in another thread on Android (crash)
This has been driving me insane for days, i am new to android and i can't seem to figure out how to access resources from other threads. I am trying to schedule a task to execute after a while, here is the relevant code:
public class TikTakBoom extends Activity {
private SensorManager mSensorManager;
public ToggleButton startButton;
private Bitmap mBombOn;
private Bitmap mBombOff;
/** Tik tak boom **/
protected MediaPlayer mediaPlayer; // play tik tak or boom
protected Timer boomTimer; // timer 开发者_如何学JAVAfor explosion
// bomb button backgrounds
protected android.graphics.drawable.BitmapDrawable buttonBombOn;
protected android.graphics.drawable.BitmapDrawable buttonBombOff;
// random boom delay in ms
protected long boomDelay = 1500;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
// tell system to use the layout defined in our XML file
setContentView(R.layout.main);
Log.w(this.getClass().getName(), "SIS is null");
// configure toggle button (bomb on/off)
startButton = (ToggleButton)findViewById(R.id.toggleButton1);
startButton.setText("");
startButton.setTextOn("");
startButton.setTextOff("");
// scale bomb images to button size
mBombOff = BitmapFactory.decodeResource(this.getResources(), R.drawable.bomb_off);
mBombOff = Bitmap.createScaledBitmap(mBombOff, 120, 120, true);
mBombOn = BitmapFactory.decodeResource(this.getResources(), R.drawable.bomb_on);
mBombOn = Bitmap.createScaledBitmap(mBombOn, 120, 120, true);
buttonBombOn = new android.graphics.drawable.BitmapDrawable(mBombOn);
buttonBombOff = new android.graphics.drawable.BitmapDrawable(mBombOff);
startButton.setChecked(false);
boomTimer = new Timer();
// onclick listener for toggle bomb on/off
startButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// Perform action on clicks
if (startButton.isChecked()) {
mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.tik_tak);
mediaPlayer.setLooping(true);
mediaPlayer.start();
// align boom delay with tik tak duration
boomDelay -= boomDelay%mediaPlayer.getDuration();
boomTimer.schedule(new BoomTimeTask(getApplicationContext()), boomDelay);
startButton.setBackgroundDrawable(buttonBombOn);
} else {
if (mediaPlayer != null) {
mediaPlayer.release();
mediaPlayer = null;
}
startButton.setBackgroundDrawable(buttonBombOff);
}
}
});
}
class BoomTimeTask extends TimerTask {
Context context;
public BoomTimeTask(Context context) {
super();
this.context = context;
}
public void run() {
if (mediaPlayer != null) {
mediaPlayer.release();
mediaPlayer = null;
}
mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.sheldon);
mediaPlayer.setLooping(false);
mediaPlayer.start();
startButton = (ToggleButton)((Activity)context).findViewById(R.id.toggleButton1);
startButton.setChecked(false);
//startButton.setBackgroundDrawable(buttonBombOff);
}
}
App crashes on startButton.setChecked(false)
in BoomTimeTask.run()
, i am guessing NullPointerException.
Pls help, i am gonna go insane, i have tried everything! Nothing works.. Tried it without the context passed as a parameter, since the reference should be visible to BoomTimeTask
thread as well, tried a dozen of other things, just can't get it to work.
What am i missing? Thx in advance, would really appreciate any kind of help. Cheers, Val.
SOLVED: how to change button text dynamically for every 3 sec in android?
You should use
runOnUiThread(new Runnable() {
public void run() {
// YOUR ACTIONS ON Graphic interface
}
});
It's because in android you can't modify GUI elements without being in the thread that deal with GUI.
http://developer.android.com/reference/android/app/Activity.html
精彩评论