Android:TextView已锁定以进行更新
我有一个开始播放mp3文件的线程,在这个线程中,我在往返之后更新我的文本视图,我使用其他thred更新我的播放器和文本视图,但在这种情况下,文本视图没有更新Android:TextView已锁定以进行更新,android,multithreading,textview,Android,Multithreading,Textview,我有一个开始播放mp3文件的线程,在这个线程中,我在往返之后更新我的文本视图,我使用其他thred更新我的播放器和文本视图,但在这种情况下,文本视图没有更新 public void updatePlayProgressUpdater() { SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this); mediaLengthIn
public void updatePlayProgressUpdater() {
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
mediaLengthInSeconds = sharedPreferences.getLong("mediaLengthInSeconds", 0);
float mediaPositionSeconds = sharedPreferences.getFloat("mediaPositionSeconds", 0);
float progress = (mediaPositionSeconds / mediaLengthInSeconds);
sb.setProgress((int) (progress * 100));
sb.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
seekChange(v);
return false;
}
});
try {
textStreamed
.setText(convertDuration(mediaPlayer.getCurrentPosition() / 1000)
+ "/" + convertDuration(mediaLengthInSeconds));
} catch (Exception e) {
Log.v("Exception","");
}
Runnable notification = new Runnable() {
public void run() {
updatePlayProgressUpdater();
}
};
handler.postDelayed(notification, 1000);
}
更换startPlayProgressUpdater;与:
创建应用程序时,有一个称为UI线程的主线程,用于处理所有UI更新。您创建的其他线程无法直接更新UI。听起来您有两个更新方法,一个从UI线程调用,另一个从后台线程调用
有许多文章比我解释得更好,例如。请发布您尝试过的代码,这样每个人都可以查看它并帮助您。try代码是否触发catch语句?无异常触发器这里是updatePlayProgressUpdater函数调用?并检查convertDurationmediaPlayer.getCurrentPosition/1000+/+convertDurationmediaLengthInSeconds方法。@Krish是的,我已检查了“mediaPlayer.getCurrentPosition/1000+/+convertDurationmediaLengthInSeconds”。。它的价值在于你解释一下为什么seekbar会被更新,而textview根本不是,但是在同一个代码中我会更新seekbar updated seccess。如果你认为代码可以工作,因为你以前使用过它,那么你的代码可能不会像你期望的那样被调用。你调试过或添加了打印语句吗?我已经一步一步地调试过了,关于你是否用更简单的东西替换你在textview中输入的东西?从PreferenceManager.GetDefaultSharedReferencesThis检索值可能有问题。另外,我在你的代码中看到3个不同的文本视图,你是否设置了所有你想要的文本视图?@Grep我已经发布了完整的代码
Runnable notification = new Runnable() {
public void run() {
startPlayProgressUpdater();
}
};
// comment ...
public void startPlayProgressUpdater() {
mediaLengthInSeconds = mediaPlayer.getDuration() / 1000;
float mediaPositionSeconds = mediaPlayer.getCurrentPosition() / 1000;
float progress = (mediaPositionSeconds / mediaLengthInSeconds);
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putLong("mediaLengthInSeconds", mediaLengthInSeconds);
editor.putFloat("mediaPositionSeconds", mediaPositionSeconds);
editor.commit(); // I missed to save the data to preference here,.
sb.setProgress((int) (progress * 100));
// playButton.setImageResource(R.drawable.pause);
sb.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
seekChange(v);
return false;
}
});
textStreamed
.setText(convertDuration(mediaPlayer.getCurrentPosition() / 1000)
+ "/" + convertDuration(mediaLengthInSeconds));
if (mediaPlayer.isPlaying()) {
Runnable notification = new Runnable() {
public void run() {
startPlayProgressUpdater();
}
};
handler.postDelayed(notification, 1000);
}
}
private void LoadPreferences(){
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
int state = sharedPreferences.getInt("stateDB", 0);
sb.setProgress(state);
textStreamed.setText(sharedPreferences.getString("statetextStreamed", ""));
coursTitle.setText(sharedPreferences.getString("coursTitle", ""));
coursIntervenant.setText(sharedPreferences.getString("coursIntervenant", ""));
new Thread() {
public void run() {
try {
runOnUiThread(new Runnable() {
@Override
public void run() {
updatePlayProgressUpdater();
}
});
Thread.sleep(300);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}.start();
}
@Override
protected void onResume() {
super.onResume();
LoadPreferences();
}
Runnable notification = new Runnable() {
public void run() {
startPlayProgressUpdater();
}
};
handler.postDelayed(notification, 1000);