Android:Thread.sleep()崩溃了应用程序

时间:2017-05-15 19:44:07

标签: java android android-thread

我想在getActivity().getSupportFragmentManager().popBackStack();之后等待,但如果我调用Thread.sleep或其他任何解决方案来等待时间,那么如果我等待1000毫秒,应用程序就会崩溃。相反,如果我等待100毫秒,应用程序不会崩溃,我不知道为什么

这是我的代码:

new Thread(new Runnable()
    {
        @Override
        public void run()
        {
            while (true)
            {
                Random random = new Random();
                mossaNemico = random.nextInt(6);
                sommaNemico = random.nextInt(11);
                if (sommaNemico >= mossaNemico)
                    break;
            }

            getActivity().getSupportFragmentManager().popBackStack();
            getActivity().getSupportFragmentManager().popBackStack();

            for(int i=0;i<99999999;i++)
            {
            } // or a Thread.sleep(1000)

                //START
                if (mossaNemico + FragmentNumeri.sceltaNumero == FragmentSomma.sceltaSomma)
                {
                    mediaPlayerVittoria = MediaPlayer.create(getActivity(), R.raw.piratasound);
                    new Thread(new Runnable()
                    {
                        boolean ciclo = true;

                        @Override
                        public void run()
                        {
                            while (ciclo)
                            {
                                mediaPlayerVittoria.setOnCompletionListener(new MediaPlayer.OnCompletionListener()
                                {
                                    @Override
                                    public void onCompletion(MediaPlayer mp)
                                    {
                                        mediaPlayerVittoria.stop();
                                        ciclo = false;
                                    }
                                });

                                if (!ciclo)
                                {
                                    break;
                                }

                                if (!mediaPlayerVittoria.isPlaying())
                                {
                                    mediaPlayerVittoria.start();
                                }
                            }
                        }
                    }).start();
                } else if (mossaNemico + FragmentNumeri.sceltaNumero == sommaNemico)
                {
                    mediaPlayerSconfitta = MediaPlayer.create(getActivity(), R.raw.esultanzasound);
                    new Thread(new Runnable()
                    {
                        boolean ciclo = true;

                        @Override
                        public void run()
                        {
                            while (ciclo)
                            {
                                mediaPlayerSconfitta.setOnCompletionListener(new MediaPlayer.OnCompletionListener()
                                {
                                    @Override
                                    public void onCompletion(MediaPlayer mp)
                                    {
                                        mediaPlayerSconfitta.stop();
                                        ciclo = false;
                                    }
                                });

                                if (!ciclo)
                                    break;

                                if (!mediaPlayerSconfitta.isPlaying())
                                {
                                    mediaPlayerSconfitta.start();
                                }
                            }
                        }
                    }).start();
                } else
                {
                    mediaPlayerStallo = MediaPlayer.create(getActivity(), R.raw.camonsound);
                    new Thread(new Runnable()
                    {
                        boolean ciclo = true;

                        @Override
                        public void run()
                        {
                            while (ciclo)
                            {
                                mediaPlayerStallo.setOnCompletionListener(new MediaPlayer.OnCompletionListener()
                                {
                                    @Override
                                    public void onCompletion(MediaPlayer mp)
                                    {
                                        mediaPlayerStallo.stop();
                                        ciclo = false;
                                    }
                                });

                                if (!ciclo)
                                    break;

                                if (!mediaPlayerStallo.isPlaying())
                                {
                                    mediaPlayerStallo.start();
                                }
                            }
                        }
                    }).start();
                }
                //FINISH
        }

    }).start();

这是我的logcat:

Process: com.example.utente.morramon, PID: 30779
                                                                         java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
                                                                             at android.media.MediaPlayer.create(MediaPlayer.java:982)
                                                                             at android.media.MediaPlayer.create(MediaPlayer.java:965)
                                                                             at com.example.utente.morramon.FragmentSomma$12.run(FragmentSomma.java:234)
                                                                             at java.lang.Thread.run(Thread.java:818)

0 个答案:

没有答案