I am having this issue on an app that I made. It's a simple game that I loads data from Room database in my onCreate
method, saves the loaded data in List<GameData> gameData
and then starts the game.
gameDao.getAll()
.subscribe(gameDatas -> {
gameData.addAll(gameData);
startGame();
});
inside my startGame
, I randomly select a number in the range [0, gameData.size()]
and do some game related work with the corresponding gameData
.
public void startGame(){
Random random = new Random();
int randomNumber = random.nextInt(gameData.size());
// do something with gameData[ramdomNumber]
}
So, then user keeps playing and then when the user loses I send him to GameOverActivity
with startActivityForResult(gameOver,1123)
Inside GameOverActivity
user can
- restart the game
- watch an ad to continue playing
- other social related staff
So when the user presses restart button, I just finish the GameOverActivity
which will redirect me to onActivityResult
of GameActivity
. Here I just call startGame
method again, and it should supposedly start a new game.
Now the problem is sometimes for certain devices, gameData
is getting empty and hence I am getting exception inside startGame
when I call random.nextInt(gameData.size())
java.lang.RuntimeException:
at android.app.ActivityThread.performResumeActivity (ActivityThread.java:3790)
at android.app.ActivityThread.handleResumeActivity (ActivityThread.java:3830)
at android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:3038)
at android.app.ActivityThread.-wrap11 (Unknown Source)
at android.app.ActivityThread$H.handleMessage (ActivityThread.java:1696)
at android.os.Handler.dispatchMessage (Handler.java:105)
at android.os.Looper.loop (Looper.java:164)
at android.app.ActivityThread.main (ActivityThread.java:6944)
at java.lang.reflect.Method.invoke (Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run (Zygote.java:327)
at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1374)
Caused by: java.lang.RuntimeException:
at android.app.ActivityThread.deliverResults (ActivityThread.java:4491)
at android.app.ActivityThread.performResumeActivity (ActivityThread.java:3762)
Caused by: java.lang.IllegalArgumentException:
at java.util.Random.nextInt (Random.java:388)
at com.company.game.GameActivity.startGame (GameActivity.java:149)
at com.company.game.GameActivity.onActivityResult (GameActivity.java:477)
at android.app.Activity.dispatchActivityResult (Activity.java:7556)
at android.app.ActivityThread.deliverResults (ActivityThread.java:4487)
I am not understating why would gameData
is getting cleared, and I am not able to check because I have never encountered while testing and I got the crash report from PlayConsole. Any idea what might be causing this?
Aucun commentaire:
Enregistrer un commentaire