splashscreen.java上的java.lang.UnsupportedOperationException错误

时间:2012-07-31 14:07:00

标签: java android eclipse stack-trace

我一直在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
    }
}

    }

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

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