Q值。我正在使用下面的代码来执行它正在执行的任务,但是当应用程序结束时,我得到一个错误,基本上说下面的线程没有正确关闭。 有谁知道如何关闭这种线程。
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!
答案 0 :(得分:3)
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);
答案 1 :(得分:1)
如果您想通过单击后退按钮完成此处理程序,请添加以下内容:
@Override
public void onBackPressed() {
// stop Handler
aHandler.removeCallbacks(r);
// to stop anonymous runnable use aHandler.removeCallbacksAndMessages(null);
}
或许你可以通过在活动onDestroy()
方法上添加它来解决这个问题:
@Override
protected void onDestroy() {
// stop Handler
aHandler.removeCallbacks(r);
// to stop anonymous runnable use aHandler.removeCallbacksAndMessages(null);
super.onDestroy();
}