1
這一直讓我瘋狂幾天,我是新來的android,我似乎無法弄清楚如何訪問其他線程的資源。我試圖安排一段時間後執行任務,這裏是有關的代碼:無法訪問Android上的另一個線程資源(崩潰)
公共類TikTakBoom延伸活動{
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 for 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);
}
}
應用在BoomTimeTask.run()
startButton.setChecked(false)
崩潰,我猜測空指針異常。
請幫助,我要去瘋了,我已經嘗試了一切!沒有任何作品..嘗試它沒有作爲參數傳遞的上下文,因爲參考應該可以看到BoomTimeTask
線程,嘗試了十幾個其他的東西,只是無法讓它的工作。
我在想什麼? Thx提前,真的會很感激任何幫助。乾杯,瓦爾。
解決:how to change button text dynamically for every 3 sec in android?
謝謝您的快速回復!我其實知道,我只是把它當作最後的手段。因爲我不想阻礙與UI無關的操作,導致UI性能下降。有沒有其他方法?或者,也許我可以拆分這個runnable並在'runOnUiThread(..)'中執行UI操作,並將其從另一個線程中分離出來以執行其他操作?乾杯,瓦爾。 – valekovski
當然在RunOnUiThread中,您只需將您的代碼與您的GUI相關=) – jDourlens
您也可以使用聽衆 – jDourlens