0

我不断收到与 splashscreen.java 中的 Thread.stop() 相关的错误。我对此很陌生,并且知道已弃用的 Thread.stop() 但有人可以解释一下我在这里做错了什么,谢谢..

    java.lang.UnsupportedOperationException
    at java.lang.Thread.stop(Thread.java:1076)
    at java.lang.Thread.stop(Thread.java:1063)
    at com.dapp.d.SplashScreen$4.run(SplashScreen.java:88)

这是 splashscreen.java 的完整源代码

    import android.app.Activity;
    import android.content.Intent;
    import android.net.Uri;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.RelativeLayout;

    public class SplashScreen extends Activity {

private boolean active = true;
private int splashTime = 3000;
private boolean clickFlag = true;
private Thread splashTread = null;
private Button btnHelp;
private Button btnAboutUs;

@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.splash);

    RelativeLayout relativeLayout = (RelativeLayout)findViewById(R.id.splashRelativeLayout);
    relativeLayout.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Uri uri = Uri.parse("http://www.exmaple.com");
            Intent intent = new Intent(Intent.ACTION_VIEW, uri);
            startActivity(intent);
        }
    });

     btnAboutUs = (Button)findViewById(R.id.btnAboutus);
     btnHelp = (Button)findViewById(R.id.btnHelp);

    try{

        btnAboutUs.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                clickFlag = false;
                splashTread.stop();  <<<<<<<<<<<<<<<<<<< line 49
                Intent intent = new Intent();
                intent.setClass(getApplicationContext(), AboutUs.class);
                startActivity(intent);
                SplashScreen.this.finish();
            }
        });

        btnHelp.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                clickFlag = false;
                splashTread.stop();   <<<<<<<<<<<<<<<<<<< line 63
                Intent intent = new Intent();
                intent.setClass(getApplicationContext(), HelpActivity.class);
                startActivity(intent);
                SplashScreen.this.finish();
            }
        });

        splashTread = new Thread() {
            @Override
            public void run() {
                try{
                    int waited = 0;
                    while(active && (waited < splashTime)) {
                        sleep(100);
                        waited += 100;
                    }                       
                } catch(InterruptedException e) {
                    // do nothing
                }finally {
                    if(clickFlag){
                        Intent intent=new Intent();
                        intent.setClass(getApplicationContext(), SearchWord.class);
                        startActivity(intent);
                        finish();
                        stop(); <<<<<<<<<<<<<<< line 88
                    }else{
                        finish();
                        stop();
                    }
                }
            }
        };
        splashTread.start();

    }catch (Exception e) {
        // TODO: handle exception
    }
}

    }
4

2 回答 2

1

我认为你根本不需要在那里停下来。您的线程不会循环,因此它将正常执行并结束。尝试删除 stop();

于 2012-07-31T14:10:35.793 回答
0

我遇到了类似的问题,解决方案是将 stop 方法包装在 try/catch 语句中,并在 catch 中使用 Throwable t。它会起作用的。

于 2012-07-31T15:10:57.480 回答