AndEngine
AndEngine copied to clipboard
BaseGameActivity crashed!
Hi, Somethimes BaseGameActivity is crashed after power off/on button is clicked. See a stacktrace: 08-27 23:22:51.160: E/AndroidRuntime(565): FATAL EXCEPTION: main 08-27 23:22:51.160: E/AndroidRuntime(565): java.lang.NullPointerException 08-27 23:22:51.160: E/AndroidRuntime(565): at org.andengine.ui.activity.BaseGameActivity.onResumeGame(BaseGameActivity.java:222) 08-27 23:22:51.160: E/AndroidRuntime(565): at org.andengine.ui.activity.BaseGameActivity$4.run(BaseGameActivity.java:373) 08-27 23:22:51.160: E/AndroidRuntime(565): at android.os.Handler.handleCallback(Handler.java:615) 08-27 23:22:51.160: E/AndroidRuntime(565): at android.os.Handler.dispatchMessage(Handler.java:92) 08-27 23:22:51.160: E/AndroidRuntime(565): at android.os.Looper.loop(Looper.java:137) 08-27 23:22:51.160: E/AndroidRuntime(565): at android.app.ActivityThread.main(ActivityThread.java:4745) 08-27 23:22:51.160: E/AndroidRuntime(565): at java.lang.reflect.Method.invokeNative(Native Method) 08-27 23:22:51.160: E/AndroidRuntime(565): at java.lang.reflect.Method.invoke(Method.java:511) 08-27 23:22:51.160: E/AndroidRuntime(565): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 08-27 23:22:51.160: E/AndroidRuntime(565): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 08-27 23:22:51.160: E/AndroidRuntime(565): at dalvik.system.NativeStart.main(Native Method)
This is because a possible race condition here. Actually, onResumeGame can be called from a separate thread in function callGameResumedOnUIThread. But to this time onDestroy can be already called for this activity and mEngine is null and new copy of Activity is created separately. Main game thread is interrupted in Engine.run() but this thread is not interrupted.
I propose to change callGameResumedOnUIThread as follow: private void callGameResumedOnUIThread() { BaseGameActivity.this.runOnUiThread(new Runnable() { @Override public void run() { if (isGameLoaded()) { BaseGameActivity.this.onResumeGame(); } } }); In other words, we will call onResume only in case when game is loaded. But if this thread is invoked after onDestroy, this call is skipped.
Maxim
I have the same problem. Solution needed.
See the same problem. In my case, some users are switching between my game activity and other game's activity, and sometimes, the mEngine may be null and activity crashes. I will try Maxim's proposal first, and see if the problem is solved and/or side effects happen.
Updated: Maxim's proposal does not work at my side.
We're having this problem to, steps to reproduce:
- Have a game that uses landscape mode only
- Start game in landscape mode
- Power off
- Power on (don't unlock yet)
- Put the device in portrait mode
- Unlock
- App crashes with that mEngine null pointer exception in BaseGameActivity
I am no entirely sure, but I think this one helps:
android:configChanges="keyboardHidden|orientation|screenSize"
android:screenOrientation="landscape"
We already have that in the manifest, and it doesn't help
<activity
android:name="eu.lafactoria.kidsPlanetDiscovery.MWActivity"
android:label="@string/app_name"
android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
android:screenOrientation="landscape" >
@Override
public synchronized void onResumeGame() {
if (this.mEngine != null)
super.onResumeGame();
}
it's fix some problems
- power off/on
- screenOrientation on load scene
Oh! yeah, that works! And isn't there any counter effect for that?
Hm... I do not have any of the issues you mention. Trying to reproduce it on:
- Sony Xperia Z tablet
- Sony Xperia Z phone
- HTC desire
- chineese tablet (I do not know the brand)
I had these issues before applying manifest changes mentioned above. I am on GLES2-AnchorCenter branch. What branch are you using?
It happened on Nexus 4, and in GLES2-AnchorCenter branch, however that (mEngine=!null) check in resumeGame worked,
Just tested on nexus4. It works fine without (mEngine=!null) check. I do not have nexus7 on hand right now, but maybe you meant this one? Nexus4 has screenlock that does not rotate, and AFAIR nexus7 has one that rotates (as most tablets).
It is strange that I do not see this issue...
It is happens on Sony Experia LT26i, Galasy S i9000 - but not allways on Samsung Ace - almost always
test from android 2.2+ to 4.1
PS: that problem exist long ago
screenlock does not relate on problem
You can try Open the first screen (eg: splash screen) on some seconds After the first screen open second screen (eg: Main Menu (load all texture in this screen)) automatically
Run game and twirl your phone until Main Screen
Hello everybody!
I know this is a very old discussion but i got the exactly same problem today and found the solution. Since it is not on this page yet, there it go!
SOLUTION, that worked for me: Put this on every activity that extends the BaseGameActivity android:configChanges="orientation|screenSize"
You should also define the orientation of the screen (If you use a fixed one), like this: android:screenOrientation="landscape"
PS: You should do this on the AndroidManifest.xml
For me, this simply solved the problem!