thread.stop()异常

时间:2012-07-05 10:39:14

标签: android multithreading android-event

我希望当用户打开应用程序播放歌曲时,当歌曲结束时,将触发一个新活动,我放一个跳过按钮,允许用户停止歌曲并转到下一个活动,这是代码

timer = new Thread() {
            public void run() {
                try {
                    sleep(13000);
                } catch (InterruptedException e) {
                    // TODO: handle exception
                } finally {
                    Intent openStartingPoing = new Intent(
                            "com.localizedbasedcompetition.SIGNIN");// action
                                                                    // name
                    startActivity(openStartingPoing);
                }
            }
        };
        timer.start();

这是跳过按钮的onclick

public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
        case R.id.bSplashSkip:
            timer.stop();
            Intent openStartingPoing = new Intent(
                    "com.localizedbasedcompetition.SIGNIN");// action name
            startActivity(openStartingPoing);
            break;

        default:
            break;
        }
    }

并且该代码工作正常,我的意思是如果我单击跳过按钮,歌曲停止并且新活动被激活,但我的问题是即使也是好的,我有一个例外(但应用程序仍然有效)和申请是

07-05 13:34:19.687: E/global(26114): Deprecated Thread methods are not supported.
07-05 13:34:19.687: E/global(26114): java.lang.UnsupportedOperationException
07-05 13:34:19.687: E/global(26114):    at java.lang.VMThread.stop(VMThread.java:85)
07-05 13:34:19.687: E/global(26114):    at java.lang.Thread.stop(Thread.java:1280)
07-05 13:34:19.687: E/global(26114):    at java.lang.Thread.stop(Thread.java:1247)
07-05 13:34:19.687: E/global(26114):    at com.localizedbasedcompetition.Splash.onClick(Splash.java:73)
07-05 13:34:19.687: E/global(26114):    at android.view.View.performClick(View.java:2552)
07-05 13:34:19.687: E/global(26114):    at android.view.View$PerformClick.run(View.java:9229)
07-05 13:34:19.687: E/global(26114):    at android.os.Handler.handleCallback(Handler.java:587)
07-05 13:34:19.687: E/global(26114):    at android.os.Handler.dispatchMessage(Handler.java:92)
07-05 13:34:19.687: E/global(26114):    at android.os.Looper.loop(Looper.java:130)
07-05 13:34:19.687: E/global(26114):    at android.app.ActivityThread.main(ActivityThread.java:3701)
07-05 13:34:19.687: E/global(26114):    at java.lang.reflect.Method.invokeNative(Native Method)
07-05 13:34:19.687: E/global(26114):    at java.lang.reflect.Method.invoke(Method.java:507)
07-05 13:34:19.687: E/global(26114):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
07-05 13:34:19.687: E/global(26114):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:624)
07-05 13:34:19.687: E/global(26114):    at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:7)

使用timer.interrupt()而不是.stop()。正如您的日志所说,不推荐使用Thread.stop()。