2

I am making one project where i need to display Home page and when home page displays, after that or continue with that 3 to 5 seconds my other welcome custom dialog is display. but making that, following error occurs but my application doesn't stop working.. LogCat displays these errors. Application Code:

  final Dialog d=new Dialog(Main.this);
    d.setContentView(R.layout.SplashScreen);
    Thread splashTread = new Thread() {
        @Override
        public void run() {
            try {
                d.show();

                int waited = 0;
                while(_active && (waited < _splashTime)) {
                    sleep(100);
                    if(_active) {
                        waited += 100;
                    }
                }
            } catch(InterruptedException e) {
                // do nothing
            } finally {
                d.cancel();
                    stop();
            }
        }
    };
    splashTread.start();
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    if (event.getAction() == MotionEvent.ACTION_DOWN) {
        _active = false;
    }
    return true;
}

The error in LogCat:

12-30 14:54:54.044: E/global(1232): Deprecated Thread methods are not supported.
12-30 14:54:54.044: E/global(1232): java.lang.UnsupportedOperationException.
12-30 14:54:54.044: E/global(1232):     at java.lang.VMThread.stop(VMThread.java:85)
12-30 14:54:54.044: E/global(1232):     at java.lang.Thread.stop(Thread.java:1280)
12-30 14:54:54.044: E/global(1232):     at java.lang.Thread.stop(Thread.java:1247)
12-30 14:54:54.044: E/global(1232):     at com.droidnova.android.SplashScreen$1.run(SplashScreen.java:35)
Dharmik
  • 2,325
  • 3
  • 27
  • 37

3 Answers3

11

In Android its better to use Handler for managing the Thread and Runnables

Create an Handler instance

Handler handler = new Handler();

Create a Runnable thread

Runnable runnable = new Runnable() {

        @Override
        public void run() {
            Log.d("runnable started", "inside run");
            handler.removeCallbacks(runnable);
            handler.postDelayed(runnable, 1000);
        }
    };

And start the Runnable using Handler

handler.postDelayed(runnable, 1000);

And to stop the Runnable use

handler.removeCallbacks(runnable);
Lalit Poptani
  • 67,150
  • 23
  • 161
  • 242
1

Put this best way to do splash screen

int SPLASH_TIME = 1300;
    Handler HANDLER = new Handler();

                    // thread for displaying the SplashScreen
                    HANDLER.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                         finish();
                         startActivity (new Intent(getApplicationContext(),Alpha.class));
                        }
                      }, SPLASH_TIME);
  • Note that this method can cause a problem of link http://stackoverflow.com/a/20905954/361100 in some android devices. – Youngjae Jan 10 '14 at 16:18
1

This link tells you exactly what the problem is, and how to resolve it:

What is this log, when I coded thread.stop()?

Thread.stop is a deprecated API, and deprecated thread methods aren't supported in Android. Therefore it's throwing an UnsupportedOperationException.

The answer is not to use Thread.stop - shut down your threads in a more graceful way, for example by setting a flag which the thread checks periodically.

This link discusses why thread.stop() is deprecated (long ago deprecated in Java, not just Android!):

http://docs.oracle.com/javase/1.4.2/docs/guide/misc/threadPrimitiveDeprecation.html

Community
  • 1
  • 1
paulsm4
  • 114,292
  • 17
  • 138
  • 190