I want to wait time after getActivity().getSupportFragmentManager().popBackStack();
but if I call Thread.sleep or any other solutions to wait time, the app crashes if I wait 1000ms. Instead if I wait 100ms, the app doesn't crashes and I don't know why
Here is my code:
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();
Here is my 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)