Thread returns an error in Android 4.0.3

I have a question that I have a splash screen in my Android application in which I use a thread to wait 8 seconds, it works fine in 1.6.2.1,2.2,2.3.3, 3.0, 3.1, but it returns an error when I I want to run the same in version 4.0.3 for Android, I don’t know why? Please suggest me the right solution for the same. Below I mentioned the error and my code.

Error Stack:

01-05 10:16:06.417: E/AndroidRuntime(589): FATAL EXCEPTION: Thread-75
01-05 10:16:06.417: E/AndroidRuntime(589): java.lang.UnsupportedOperationException
01-05 10:16:06.417: E/AndroidRuntime(589):  at java.lang.Thread.stop(Thread.java:1076)
01-05 10:16:06.417: E/AndroidRuntime(589):  at java.lang.Thread.stop(Thread.java:1063)
01-05 10:16:06.417: E/AndroidRuntime(589):  at com.shipface.common.SplashScreen$1.run(SplashScreen.java:34)

The code:

public class SplashScreen extends Activity {
    /** Called when the activity is first created. */
    Thread splash;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        splash =  new Thread(){
            @Override
            public void run(){
                try {

                    synchronized(this){
                    // Wait given period of time or exit on touch
                        wait(4000);
                        Intent intent = new Intent(SplashScreen.this,HomeActivity.class);
                        startActivity(intent);
                        finish();
                    }
                }
                catch(InterruptedException ex){                    
                }

                finish();


                stop();                    
            }
        };


        splash.start();   
    }
}
+5
source share
4 answers

Everyone has already said where the exception ( Thread.stop()) comes from , so I will leave it alone ...

Thread ; AsyncTask . , Handler ( CountDownTimer, Handler , IMO).

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Handler handler = new Handler();
    Runnable action = new Runnable(){
        @Override
        public void run(){
            Intent intent = new Intent(SplashScreen.this,HomeActivity.class);
            startActivity(intent);
            finish();
        }
    };

    handler.postDelayed(action, 8000);  
}

Handler , .

+3

Thread.stop - , - . . AsyncTask .

+1

, stop() Thread, stop() , Thread, thread_instance.interrupt(). ,

new Thread(new Runnable() {

            @Override
            public void run() {

                try {
                    Thread.sleep(4000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                Intent intent = new Intent(SplashScreen.this,HomeActivity.class);
                startActivity(intent);
                SplashScreen.this.finish();
            }
        }).start();
+1

The stop () method is deprecated.
In earlier versions it UnsupportedOperationExceptionwas simply ignored, however you can see it in the log. But in 4.0.3 this is considered a fatal exception.
You can remove the call to stop () in the code. It is not necessary.

+1
source

All Articles