有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

安卓 java。屏幕上的lang.UnsupportedOperationException错误。JAVA

我不断得到与线程相关的错误。在splashscreen中停止()。JAVA我对它很陌生,并且知道不推荐使用的线程。请有人解释一下我做错了什么,谢谢

    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的完整源代码。爪哇

    import 安卓.app.Activity;
    import 安卓.content.Intent;
    import 安卓.net.Uri;
    import 安卓.os.Bundle;
    import 安卓.view.View;
    import 安卓.view.View.OnClickListener;
    import 安卓.widget.Button;
    import 安卓.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) 个答案

  1. # 1 楼答案

    我有一个类似的问题,解决方法是将stop方法包装在try/catch语句和catch use Throwable t中。 它会起作用的

  2. # 2 楼答案

    我认为你根本不需要打电话到那里。您的线程不会循环,因此它将正常执行并结束。试着卸下挡块()