Splash screen thread throwing errors. How to resolve? (Code & Errors Included)

可紊 提交于 2019-12-25 03:54:11

问题


I have some errors with my splash screen that I have been working on for a while and can't figure out. Is there a better way to time my splash screen than a thread? What's wrong with my current thread? Can you see an issue with my media player object?

I've posted the guts of my splash class. Hopefully I can get some direction on these issues. This works when I run the app but I just don't want to have errors.

-------------------------Code------------------------------

@Override
public void onCreate(Bundle savedInstanceState) {
 ......onCreate, hide window, and setting content view.......
        // Play Sound for startup
        mpSplash = MediaPlayer.create(this, R.raw.splashscream);
        mpSplash.start();
        final Splash splash = this;
        logoTimer = new Thread(){
        public void run(){
            try {
                synchronized(this){
                    // Wait given period of time or exit on touch
                    wait(4500);
                }
            } 
            catch(InterruptedException ex){ 
                ex.printStackTrace();
            }
            finish();
            mpSplash.stop();
            mpSplash.reset();
            //mpSplash.release();
            //mpSplash.release();
            // Run next activity
            Intent intent = new Intent();
            intent.setClass(splash, Game.class);
            startActivity(intent);
            stop();
        }
    };
    logoTimer.start();
}
// Splash screen touch events
@Override
public boolean onTouchEvent (MotionEvent evt)
{
    if(evt.getAction() == MotionEvent.ACTION_DOWN)
    {
        // Stop the introduction sounds
        mpSplash.stop();
        mpSplash.reset();
        //mpSplash.release();
        //mpSplash.release();
        synchronized(logoTimer){
            logoTimer.notifyAll();
        }
    }
    return true;
}

------------------------------Errors-----------------------------

09-11 21:50:04.644: ERROR/MediaPlayer(460): stop called in state 1
09-11 21:50:04.644: ERROR/MediaPlayer(460): error (-38, 0)
09-11 21:50:04.654: ERROR/global(460): Deprecated Thread methods are not supported.
09-11 21:50:04.654: ERROR/global(460): java.lang.UnsupportedOperationException
09-11 21:50:04.654: ERROR/global(460):     at java.lang.VMThread.stop(VMThread.java:85)
09-11 21:50:04.654: ERROR/global(460):     at java.lang.Thread.stop(Thread.java:1379)
09-11 21:50:04.654: ERROR/global(460):     at java.lang.Thread.stop(Thread.java:1344)
09-11 21:50:04.654: ERROR/global(460):     at com.ss.lastzombie.Splash$1.run(Splash.java:61)

Thanks!!


回答1:


Don't call stop() in your thread. That's a deprecated method (it leads to instability in the VM) and is not needed. (The thread will exit when the run() method returns). You probably intended to call finish() for the splash activity. That would make sense.

Just for form's sake, you might want to call startActivity and finish on the main thread instead of your worker thread. To do this, post a Runnable using runOnUIThread() and call those two methods from the Runnable.



来源:https://stackoverflow.com/questions/7381637/splash-screen-thread-throwing-errors-how-to-resolve-code-errors-included

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!