AndEngine icon indicating copy to clipboard operation
AndEngine copied to clipboard

BaseGameActivity crashed!

Open muhanov opened this issue 12 years ago • 14 comments

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

muhanov avatar Aug 27 '12 19:08 muhanov

I have the same problem. Solution needed.

oleonov avatar Oct 22 '12 17:10 oleonov

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.

yhuiyang avatar Nov 16 '12 13:11 yhuiyang

We're having this problem to, steps to reproduce:

  1. Have a game that uses landscape mode only
  2. Start game in landscape mode
  3. Power off
  4. Power on (don't unlock yet)
  5. Put the device in portrait mode
  6. Unlock
  7. App crashes with that mEngine null pointer exception in BaseGameActivity

lafactoria avatar Jun 25 '13 08:06 lafactoria

I am no entirely sure, but I think this one helps:

android:configChanges="keyboardHidden|orientation|screenSize"
android:screenOrientation="landscape"

nazgee avatar Jun 25 '13 09:06 nazgee

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"  >

lafactoria avatar Jun 25 '13 09:06 lafactoria

@Override
public synchronized void onResumeGame() {
    if (this.mEngine != null)
        super.onResumeGame();
}

maksimln avatar Jun 25 '13 09:06 maksimln

it's fix some problems

  1. power off/on
  2. screenOrientation on load scene

maksimln avatar Jun 25 '13 09:06 maksimln

Oh! yeah, that works! And isn't there any counter effect for that?

lafactoria avatar Jun 25 '13 09:06 lafactoria

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?

nazgee avatar Jun 25 '13 09:06 nazgee

It happened on Nexus 4, and in GLES2-AnchorCenter branch, however that (mEngine=!null) check in resumeGame worked,

lafactoria avatar Jun 25 '13 09:06 lafactoria

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...

nazgee avatar Jun 25 '13 09:06 nazgee

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

maksimln avatar Jun 25 '13 09:06 maksimln

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

maksimln avatar Jun 25 '13 10:06 maksimln

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!

mfbottoni avatar Mar 28 '14 20:03 mfbottoni