Java Plugin, Nullreference on resume

Hello,

I have a Unity project with a Java Android plugin. The enrty point for the app is a UnityPlayerActivity in this plugin, which starts up the actual UnityPlayer and adds this to a native layout that I have control over.

This works fine until I minimise and maximise the app (onPause → onStop → onRestart ->onStart → onResume), at which point the app crashes and logcat gives me the following stacktrace :

14:58:11.145 Error AndroidRuntime 12565  FATAL EXCEPTION: main
14:58:11.145 Error AndroidRuntime 12565  java.lang.Error: FATAL EXCEPTION [main]
14:58:11.146 Error AndroidRuntime 12565  Unity version     : 4.1.2f1
14:58:11.146 Error AndroidRuntime 12565  Device model      : asus ASUS Transformer Pad TF300T
14:58:11.147 Error AndroidRuntime 12565  Device fingerprint: asus/WW_epad/TF300T:4.0.3/IML74K/WW_epad-9.4.3.30-20120604:user/release-keys
14:58:11.147 Error AndroidRuntime 12565 
14:58:11.148 Error AndroidRuntime 12565  Caused by: java.lang.RuntimeException: Unable to resume activity {my.android.game/my.android.game.ViewActivityUnity}: java.lang.NullPointerException
14:58:11.149 Error AndroidRuntime 12565  	at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2444)
14:58:11.149 Error AndroidRuntime 12565  	at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2472)
14:58:11.150 Error AndroidRuntime 12565  	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1173)
14:58:11.150 Error AndroidRuntime 12565  	at android.os.Handler.dispatchMessage(Handler.java:99)
14:58:11.151 Error AndroidRuntime 12565  	at android.os.Looper.loop(Looper.java:137)
14:58:11.151 Error AndroidRuntime 12565  	at android.app.ActivityThread.main(ActivityThread.java:4424)
14:58:11.152 Error AndroidRuntime 12565  	at java.lang.reflect.Method.invokeNative(Native Method)
14:58:11.152 Error AndroidRuntime 12565  	at java.lang.reflect.Method.invoke(Method.java:511)
14:58:11.153 Error AndroidRuntime 12565  	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
14:58:11.154 Error AndroidRuntime 12565  	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
14:58:11.154 Error AndroidRuntime 12565  	at dalvik.system.NativeStart.main(Native Method)
14:58:11.155 Error AndroidRuntime 12565  Caused by: java.lang.NullPointerException
14:58:11.155 Error AndroidRuntime 12565  	at com.unity3d.player.g.a(Unknown Source)
14:58:11.156 Error AndroidRuntime 12565  	at com.unity3d.player.g.b(Unknown Source)
14:58:11.156 Error AndroidRuntime 12565  	at com.unity3d.player.UnityPlayer.resume(Unknown Source)
14:58:11.157 Error AndroidRuntime 12565  	at com.unity3d.player.UnityPlayerActivity.onResume(Unknown Source)
14:58:11.157 Error AndroidRuntime 12565  	at my.android.game.ViewActivityUnity.onResume(ViewActivityUnity.java:53)
14:58:11.158 Error AndroidRuntime 12565  	at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1154)
14:58:11.159 Error AndroidRuntime 12565  	at android.app.Activity.performResume(Activity.java:4539)
14:58:11.159 Error AndroidRuntime 12565  	at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2434)
14:58:11.160 Error AndroidRuntime 12565  	... 10 more

Looking closer, this is the bit that interests me

14:58:11.155 Error AndroidRuntime 12565  Caused by: java.lang.NullPointerException
14:58:11.155 Error AndroidRuntime 12565  	at com.unity3d.player.g.a(Unknown Source)
14:58:11.156 Error AndroidRuntime 12565  	at com.unity3d.player.g.b(Unknown Source)
14:58:11.156 Error AndroidRuntime 12565  	at com.unity3d.player.UnityPlayer.resume(Unknown Source)
14:58:11.157 Error AndroidRuntime 12565  	at com.unity3d.player.UnityPlayerActivity.onResume(Unknown Source)
14:58:11.157 Error AndroidRuntime 12565  	at my.android.game.ViewActivityUnity.onResume(ViewActivityUnity.java:53)

the only mention of my code is my.android.game.ViewActivityUnity.onResume(ViewActivityUnity.java:53), that line only contains super.onResume();

protected void onResume()
{	
	Log.d("MyMessage", "onResume");
	super.onResume();
}

Has anyone had any experience with this ? To me it seems like the unityplayer can’t resume itself when created from user code (because certain fields in unity’s internal code get into a null state which gives me a nullreference from unity’s code) but I could be completely wrong.

I create the UnityPlayer with the following code, maybe I made a huge mistake here though i took this from code Unity generates itself when you export your Unity project as an Eclipse project (and it works perfectly, until you minimise and maximise the app).

private void createUnityView()
{
	m_unityPlayer = new UnityPlayer(this);
	getWindow().takeSurface(null);
	
	setTheme(android.R.style.Theme_NoTitleBar_Fullscreen);
	getWindow().setFormat(PixelFormat.RGB_565);

	if (m_unityPlayer.getSettings ().getBoolean ("hide_status_bar", true))
		getWindow ().setFlags (WindowManager.LayoutParams.FLAG_FULLSCREEN,
			       WindowManager.LayoutParams.FLAG_FULLSCREEN);

	int glesMode = m_unityPlayer.getSettings().getInt("gles_mode", 1);
	boolean trueColor8888 = false;
	m_unityPlayer.init(glesMode, trueColor8888);

	m_unityView = m_unityPlayer.getView();
	m_unityLayout.addView(m_unityView);
}

Can anyone help me with this issue, please ?
Is there prehaps a way to completely quit and restart the UnityPlayer without destroying the current UnityPlayerActivity ? That way i could save the state of my unity view in onPause and manually restore this during onResume

Could be related to this:
http://forum.unity3d.com/threads/127794-Android-Apps-crashing-on-resume

But you’re on a Transformer… so maybe not. Worth a try though.

Thanks, JoshOClock your information seemed helpful but sadly did not work. I already had android:configChanges defined in my AndroidManifest.xml. I tried adding it to the plugin’s AndroidManifest.xml just to be on the safe side and that did not change anything. I then triggered an onConfigurationChanged event (by turning the device around, making it go from landscape to portrait mode) and the crash didn’t happen so I’m afraid these are two seperate issues.

I fixed it by surrounding super.onResume() with a try/catch block but this is obviously super hacky. The weird part is everything seems to work properly so the exception is still being thrown in my UnityPlayerActivity baseclass’ onResume(), I just catch it before it can reach the surface. So everything works when really it shouldn’t :stuck_out_tongue:
I’d still like to sort this out in a proper way so all ideas are welcome.