gpt4 book ai didi

java - runOnUiThread 如何关闭/关闭线程

转载 作者:行者123 更新时间:2023-11-29 07:45:37 27 4
gpt4 key购买 nike

问。我正在使用下面的代码来执行它正在执行的任务,但是当应用程序结束时我收到一个错误,基本上说下面的线程没有正确关闭。有谁知道如何关闭这种线程。

private Handler aHandler = new Handler();
private final Runnable r = new Runnable()
{
@Override
public void run()
{
updateProgress();
}
};

private void updateProgress()
{
if (scrubber != null)
Log.d("TAG", ...);

aHandler.postDelayed(r, 1000);
}

// IN THE ON CREATE METHOD I USE

runOnUiThread(new Runnable()
{
public void run()
{
updateProgress();
}
});

谢谢你的帮助,但我仍然收到错误,如下所示:

10-01 14:26:29.978: I/SurfaceView(19852): updateWindow -- OnPreDrawListener, mHaveFrame = true
10-01 14:26:29.978: D/dalvikvm(19852): threadid=15: interp stack at 0x60be9000
10-01 14:26:29.979: D/AndroidRuntime(19852): Shutting down VM
10-01 14:26:29.979: W/dalvikvm(19852): threadid=1: thread exiting with uncaught exception (group=0x41f76c98)
10-01 14:26:29.979: W/dalvikvm(19852): threadid=1: uncaught exception occurred
10-01 14:26:29.979: W/System.err(19852): java.lang.IllegalStateException
10-01 14:26:29.980: W/System.err(19852): at android.media.MediaPlayer.isPlaying(Native Method)
10-01 14:26:29.980: W/System.err(19852): at com.aviit.bluebox.MediaActivity.updateProgress(MediaActivity.java:162)
10-01 14:26:29.980: W/System.err(19852): at com.aviit.bluebox.MediaActivity.access$0(MediaActivity.java:157)
10-01 14:26:29.980: W/System.err(19852): at com.aviit.bluebox.MediaActivity$1.run(MediaActivity.java:153)
10-01 14:26:29.980: W/System.err(19852): at android.os.Handler.handleCallback(Handler.java:808)
10-01 14:26:29.980: W/System.err(19852): at android.os.Handler.dispatchMessage(Handler.java:103)
10-01 14:26:29.980: W/System.err(19852): at android.os.Looper.loop(Looper.java:193)
10-01 14:26:29.980: W/System.err(19852): at android.app.ActivityThread.main(ActivityThread.java:5292)
10-01 14:26:29.981: W/System.err(19852): at java.lang.reflect.Method.invokeNative(Native Method)
10-01 14:26:29.981: W/System.err(19852): at java.lang.reflect.Method.invoke(Method.java:515)
10-01 14:26:29.981: W/System.err(19852): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
10-01 14:26:29.981: W/System.err(19852): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
10-01 14:26:29.981: W/System.err(19852): at dalvik.system.NativeStart.main(Native Method)
10-01 14:26:29.981: W/dalvikvm(19852): threadid=1: calling UncaughtExceptionHandler
10-01 14:26:29.982: E/AndroidRuntime(19852): FATAL EXCEPTION: main
10-01 14:26:29.982: E/AndroidRuntime(19852): Process: com.aviit.bluebox, PID: 19852
10-01 14:26:29.982: E/AndroidRuntime(19852): java.lang.IllegalStateException
10-01 14:26:29.982: E/AndroidRuntime(19852): at android.media.MediaPlayer.isPlaying(Native Method)
10-01 14:26:29.982: E/AndroidRuntime(19852): at com.aviit.bluebox.MediaActivity.updateProgress(MediaActivity.java:162)
10-01 14:26:29.982: E/AndroidRuntime(19852): at com.aviit.bluebox.MediaActivity.access$0(MediaActivity.java:157)
10-01 14:26:29.982: E/AndroidRuntime(19852): at com.aviit.bluebox.MediaActivity$1.run(MediaActivity.java:153)
10-01 14:26:29.982: E/AndroidRuntime(19852): at android.os.Handler.handleCallback(Handler.java:808)
10-01 14:26:29.982: E/AndroidRuntime(19852): at android.os.Handler.dispatchMessage(Handler.java:103)
10-01 14:26:29.982: E/AndroidRuntime(19852): at android.os.Looper.loop(Looper.java:193)
10-01 14:26:29.982: E/AndroidRuntime(19852): at android.app.ActivityThread.main(ActivityThread.java:5292)
10-01 14:26:29.982: E/AndroidRuntime(19852): at java.lang.reflect.Method.invokeNative(Native Method)
10-01 14:26:29.982: E/AndroidRuntime(19852): at java.lang.reflect.Method.invoke(Method.java:515)
10-01 14:26:29.982: E/AndroidRuntime(19852): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
10-01 14:26:29.982: E/AndroidRuntime(19852): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
10-01 14:26:29.982: E/AndroidRuntime(19852): at dalvik.system.NativeStart.main(Native Method)
10-01 14:27:02.097: D/dalvikvm(19852): threadid=12: exiting
10-01 14:27:02.097: D/dalvikvm(19852): threadid=12: bye!

最佳答案

private Handler aHandler = new Handler();
private final Runnable r = new Runnable()
{
@Override
public void run()
{

if (scrubber != null)
Log.d("TAG", ...);

aHandler.postDelayed(this, 1000);
}
};

在代码某处添加:

  aHandler.post(r); 

完成后,您需要:

  aHandler.removeCallback(r);

关于java - runOnUiThread 如何关闭/关闭线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26142051/

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