Java 在Runnable中更新TextView会导致活动停止
我有一个mediaplayer服务,其中我有一个Runnable,它每100毫秒更新一次seekbar和currentposition文本视图Java 在Runnable中更新TextView会导致活动停止,java,android,runnable,Java,Android,Runnable,我有一个mediaplayer服务,其中我有一个Runnable,它每100毫秒更新一次seekbar和currentposition文本视图 private Runnable mUpdateTimeTask = new Runnable() { public void run() { long totalDuration = mediaPlayer.getDuration(); long currentDuration = med
private Runnable mUpdateTimeTask = new Runnable() {
public void run() {
long totalDuration = mediaPlayer.getDuration();
long currentDuration = mediaPlayer.getCurrentPosition();
seekBar.setProgress(progress);
nowduration.setText("" + utils.milliSecondsToTimer(currentDuration));
totalduration.setText(""+utils.milliSecondsToTimer(totalDuration));
// Running this thread after 100 milliseconds
mHandler.postDelayed(this, 100);
}
};
问题是,经过一段时间(5-10秒)活动停止后,我在logcat中看到的唯一错误是。删除nowduration.setText
时一切正常,seekbar正在更新,没有任何问题,为什么TextView活动会停止
04-10 10:22:40.610 288-813/system_process I/ActivityManager﹕ Process com.package.appname (pid 3734) has died.
04-10 10:22:40.610 288-3633/system_process I/WindowManager﹕ WIN DEATH: Window{424b5708 com.package.appname/com.package.appname.MainActivity paused=false}
我尝试的是:1。在TextView.setText上使用runOnUithread-相同的行为2。使用线程而不是处理程序-但我在停止线程时遇到问题,有时我需要调用
处理程序.removeCallBacks
,但正如我所知,停止线程是一个非常糟糕的操作,这就是为什么Thread.stop(被折旧)的原因。用户界面只能由UI线程更新。您需要一个处理程序来发布到UI线程:
private void startTimerThread() {
Handler handler = new Handler();
Runnable runnable = new Runnable() {
private long startTime = System.currentTimeMillis();
public void run() {
try {
Thread.sleep(1000);
}
catch (InterruptedException e) {
e.printStackTrace();
}
handler.post(new Runnable(){
public void run() {
long totalDuration = mediaPlayer.getDuration();
long currentDuration = mediaPlayer.getCurrentPosition();
seekBar.setProgress(progress);
nowduration.setText("" + utils.milliSecondsToTimer(currentDuration));
totalduration.setText(""+utils.milliSecondsToTimer(totalDuration));
}
});
}
};
new Thread(runnable).start();
}
尝试使用处理程序消息来执行此操作:
private Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch(msg.what){
case 1:{
long totalDuration = mediaPlayer.getDuration();
long currentDuration = mediaPlayer.getCurrentPosition();
seekBar.setProgress(progress);
nowduration.setText("" + utils.milliSecondsToTimer(currentDuration));
totalduration.setText(""+utils.milliSecondsToTimer(totalDuration));
// Running this thread after 100 milliseconds
mHandler.sendEmptyMessageDelayed(1, 100);
}break;
}
}
};
第一次更新开始:
...
handler.sendEmptyMessage(1);
...
您是否使用处理程序以外的线程?您可以使用
处理程序.removeCallBacks
。我认为您误解了handler的用法,并发布了完整的StackTrace。我在op的代码中没有看到背景线程。Op正在使用一个处理程序<代码>mHandler.postDelayed(这是100)代码>。如果在ui中创建处理程序,则其线程将与该处理程序关联。引用当你创建一个新的处理程序时,它被绑定到正在创建它的线程/消息队列——从那时起,它将把消息和可运行文件传递到该消息队列,并在它们从消息队列中出来时执行它们。嗯。。。看起来像是Android操作系统的Bug。。。也许这篇文章可以帮助你,我不知道为什么,但这给了我和我的代码一样的行为。活动刚刚结束。我认为这是内存泄漏,但不知道为什么。也许这是我手机的问题?您的播放器是否在您出错之前完成播放?否,活动停止,媒体播放器服务也停止。问题出在textView中,因为在删除textView.setText()时,您的代码工作正常。