gpt4 book ai didi

java - RecyclerView Adapter - 只有创建 View 层次结构的原始线程才能触摸其 View

转载 作者:行者123 更新时间:2023-12-01 22:13:04 25 4
gpt4 key购买 nike

我的应用程序有一个 RecyclerView,我将在其中显示一个音频播放器。我想在播放音频时显示当前的音频时间,但是在播放时 android 会出现以下错误:

E/AndroidRuntime: FATAL EXCEPTION: Thread-4
Process: myapp.myapp.com.myapp, PID: 24800
android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:7154)
at android.view.ViewRootImpl.invalidateChildInParent(ViewRootImpl.java:1128)
at android.view.ViewGroup.invalidateChild(ViewGroup.java:5286)
at android.view.View.invalidateInternal(View.java:13616)
at android.view.View.invalidate(View.java:13580)
at android.view.View.invalidate(View.java:13564)
at android.widget.TextView.checkForRelayout(TextView.java:7616)
at android.widget.TextView.setText(TextView.java:4730)
at android.widget.TextView.setText(TextView.java:4587)
at android.widget.TextView.setText(TextView.java:4542)
at myapp.myapp.com.myapp.adapter.MyAdapter$12$1.run(MyAdapter.java:882)
at java.lang.Thread.run(Thread.java:761)

onBindViewHolder 内部的方法:

    private void reproduzirAudio(MyViewHolder h, File arquivoAudio) {

h.playAudioChat.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

//Criar player
if (h.mediaPlayer == null) {
h.mediaPlayer = MediaPlayer.create(context, Uri.fromFile(arquivoAudio));
h.mediaPlayer.setLooping(false);
h.mediaPlayer.seekTo(0);

//Configurar playButton
if (!h.mediaPlayer.isPlaying()) {
//stopping
h.mediaPlayer.start();
h.playAudioChat.setImageResource(R.drawable.ic_pause_circle);
} else {
h.mediaPlayer.pause();
h.playAudioChat.setImageResource(R.drawable.ic_play_circle_white);
}

//Audiowave
h.audioThread = new Thread(new Runnable() {
@Override
public void run() {
while (h.mediaPlayer != null){
if (h.mediaPlayer.isPlaying()) {
try {

//Audiowave
float waveProgress = h.mediaPlayer.getCurrentPosition()
/ (float) h.mediaPlayer.getDuration() * 100F;
h.audioWaveView.setProgress(waveProgress);
Log.i("WAVE",String.valueOf(waveProgress));

//Timer
h.timeAudioChat.setText(formatarTempoAudio(h.mediaPlayer.getCurrentPosition()));

Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
});
h.audioThread.start();
//...
}
}
});
}
private String formatarTempoAudio(long currentPosition) {
String finalTimerString = "";
String secondsString = "";
int hours = (((int) currentPosition / (1000 * 60 * 60)));
int minutes = ((int) currentPosition % (1000 * 60 * 60)) / (1000 * 60);
int seconds = (((int) currentPosition % (1000 * 60 * 60)) % (1000 * 60) / 1000);
// Add hours if there
if (hours > 0) {
finalTimerString = hours + ":";
}
if (seconds < 10) {
secondsString = "0" + seconds;
} else {
secondsString = "" + seconds;
}
finalTimerString = finalTimerString + minutes + ":" + secondsString;
return finalTimerString;
}

在线运行时出错h.timeAudioChat.setText(formatTempoAudio(h.mediaPlayer.getCurrentPosition()));

我相信这个更新应该在 UI 线程中完成,但由于我在 RecyclerViewAdapter 上,所以我无法访问它。如何使用当前音频时间更新 TextView?提前致谢!

最佳答案

尽管代码的组织是可以改进的,因为您在 ViewHolder 参数内有上下文的引用,但您可以通过这种方式在 UI 线程内运行:

Handler handlerInMainThread = new Handler(itemView.getContext().getMainLooper());

Runnable yourRunnable = new Runnable() {
@Override
public void run() {
// Put the desired UI code here
}
}

handlerInMainThread.post(yourRunnable);

This是一个很好的答案,有类似的问题,并且 this涵盖访问 UI 的不同方式。 android docs 中也有文档.

关于java - RecyclerView Adapter - 只有创建 View 层次结构的原始线程才能触摸其 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58643720/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com