2

我想在点击启动画面时停止正在运行的线程,如果我不点击屏幕,在线程执行后,它将启动另一个活动。但是得到UnSupportedException,我该如何解决呢?

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_splash);
    iImage = (ImageView) findViewById(R.id.iIcon);
    splashImage = (ImageView) findViewById(R.id.splash_image);

    iImage.setOnClickListener(this);
    splashImage.setOnClickListener(this);
}

@Override
protected void onStart() {
    super.onStart();
    splashTimer = new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            startActivity(new Intent(SplashActivity.this, LoginAuthenticationActivity.class));
            finish();
        }
    });
    splashTimer.start();
}

@Override
public void onClick(View view) {
    if(splashTimer.isAlive())
        splashTimer.stop();
    switch (view.getId()) {
        case R.id.iIcon:
            startActivity(new Intent(this, AboutUsActivity.class));
            break;
        case R.id.splash_image:
            startActivity(new Intent(this, LoginAuthenticationActivity.class));
            break;
        default:
            break;
    }
    finish();
}

日志:

01-27 03:27:01.189: W/dalvikvm(1080): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
01-27 03:27:01.209: E/AndroidRuntime(1080): FATAL EXCEPTION: main
01-27 03:27:01.209: E/AndroidRuntime(1080): java.lang.UnsupportedOperationException
01-27 03:27:01.209: E/AndroidRuntime(1080):     at java.lang.Thread.stop(Thread.java:1076)
01-27 03:27:01.209: E/AndroidRuntime(1080):     at java.lang.Thread.stop(Thread.java:1063)
01-27 03:27:01.209: E/AndroidRuntime(1080):     at com.app.wooqer.SplashActivity.onClick(SplashActivity.java:48)
01-27 03:27:01.209: E/AndroidRuntime(1080):     at android.view.View.performClick(View.java:3511)
01-27 03:27:01.209: E/AndroidRuntime(1080):     at android.view.View$PerformClick.run(View.java:14105)
01-27 03:27:01.209: E/AndroidRuntime(1080):     at android.os.Handler.handleCallback(Handler.java:605)
01-27 03:27:01.209: E/AndroidRuntime(1080):     at android.os.Handler.dispatchMessage(Handler.java:92)
01-27 03:27:01.209: E/AndroidRuntime(1080):     at android.os.Looper.loop(Looper.java:137)
01-27 03:27:01.209: E/AndroidRuntime(1080):     at android.app.ActivityThread.main(ActivityThread.java:4424)
01-27 03:27:01.209: E/AndroidRuntime(1080):     at java.lang.reflect.Method.invokeNative(Native Method)
01-27 03:27:01.209: E/AndroidRuntime(1080):     at java.lang.reflect.Method.invoke(Method.java:511)
01-27 03:27:01.209: E/AndroidRuntime(1080):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
01-27 03:27:01.209: E/AndroidRuntime(1080):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
01-27 03:27:01.209: E/AndroidRuntime(1080):     at dalvik.system.NativeStart.main(Native Method)
4

1 回答 1

1

您正在做的事情非常浪费(任何启动画面都是浪费,但使用这样的线程更是如此),但要解决您的问题:

使用interrrupt();而不是stop();

正如文档所说stop()

Throws UnsupportedOperationException.

要解决重复问题,请移动startActivity()内部,try使其看起来像这样:

public void run() {
  try {
    Thread.sleep(5000);
    startActivity(new Intent(SplashActivity.this, LoginAuthenticationActivity.class));
  } catch (InterruptedException e) {
    e.printStackTrace();
  }
  finish();
}

这样,当您调用interrupt()所有 Activity时,不会调用finish()副本startActivity()

进一步说明:

第一个问题:stop()默认情况下抛出异常,因为它是您不应该使用的不安全方法。

然后,当您使用 时interrupt(),您已经在catch 块之后startActivity()的 run 方法中。当你打断时,被调用一次和一次。通过在块内移动到 之后,当中断时,块的其余部分不会被执行。这意味着您现在只有一个电话,而不是两个。有关更多信息,请阅读异常startActivity()run()onClick()startActivity()tryThread.sleep()interrupt()ThreadtrystartActivity()

于 2013-01-26T22:02:59.233 回答