Activity being initiated twice

Hi,



I got my main activity class that extends androidharness.

The problem is that it’s being created more than once!

I figured something like this was the source of my nifty problems, so I created some roadblocks.

For each class that have a static method to get the instance of the class, I’v added a block to throw an exception of an instance already exsist.

So anyways, it turns out my main activity throws that exception, and I don’t know why. No reference to the constructor of my class exsist in any of my java files, so it must be jme3 that somehow calls it.



Here’s my activity:

[java]package andrew.planetdash;



import start.MainClass;

import android.content.pm.ActivityInfo;

import android.util.Log;

import android.view.MotionEvent;



import com.jme3.app.AndroidHarness;

import com.jme3.system.android.AndroidConfigChooser.ConfigType;



public class AndroidDashActivity extends AndroidHarness{

private static AndroidDashActivity thais = null;

private long time;





public AndroidDashActivity() throws Exception{// void onCreate(Bundle savedInstanceState) {



Log.d(“shit”, “Starting”);

// Set the application class to run

appClass = “start.MainClass”;

// appClass = “jme3test.android.SimpleTexturedTest”;

eglConfigType = ConfigType.FASTEST; // Default

// Edit 22.06.2011: added a switch to get the highest (best) egl config

// available on this device

// Its usually RGBA8888

// eglConfigType = ConfigType.BEST;

// Exit Dialog title & message

exitDialogTitle = “Exit?”;

exitDialogMessage = “Do you wish to exit?”;

// Edit: 25.06.2011: Enable verbose logging

eglConfigVerboseLogging = true;

// Edit: 30.06.2011: Choose screen orientation

screenOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;

// Edit 12.07.2011: Invert the MouseEvents X (default = true)

mouseEventsInvertX = true;

// Edit 05.07.2011: Invert the MouseEvents Y (default = true)

mouseEventsInvertY = true;



if (thais != null){

throw new Exception(“Multiple instance of class AndroidDashActivity is NOT ALLOWED.”);

}

thais = this;



}



public static AndroidDashActivity getThis(){

return thais;

}



/**

  • Handles input that adjust the boosting and removal of the rocket.

    */

    @Override

    public boolean onTrackballEvent(MotionEvent event){

    int action = event.getAction();

    if (action == MotionEvent.ACTION_DOWN){

    if (MainClass.base.getRocket() == null){

    MainClass.base.doLaunch = true;

    } else {

    long newTime = System.currentTimeMillis();

    if (newTime - time < 200){

    MainClass.base.getRocket().remove = true;

    } else {

    MainClass.base.getRocket().boost();

    }

    time = newTime;

    }

    }

    if (action == MotionEvent.ACTION_UP){

    if (MainClass.base.getRocket() != null){

    MainClass.base.getRocket().stopBoost();

    }

    }



    return true;

    }

    }[/java]



    And heres the stacktrace:

    [patch]02-25 18:28:32.361: E/AndroidRuntime(3821): FATAL EXCEPTION: main

    02-25 18:28:32.361: E/AndroidRuntime(3821): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{andrew.planetdash/andrew.planetdash.AndroidDashActivity}: java.lang.Exception: Multiple instance of class AndroidDashActivity is NOT ALLOWED.

    02-25 18:28:32.361: E/AndroidRuntime(3821): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1581)

    02-25 18:28:32.361: E/AndroidRuntime(3821): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1675)

    02-25 18:28:32.361: E/AndroidRuntime(3821): at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:2853)

    02-25 18:28:32.361: E/AndroidRuntime(3821): at android.app.ActivityThread.access$1600(ActivityThread.java:121)

    02-25 18:28:32.361: E/AndroidRuntime(3821): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:947)

    02-25 18:28:32.361: E/AndroidRuntime(3821): at android.os.Handler.dispatchMessage(Handler.java:99)

    02-25 18:28:32.361: E/AndroidRuntime(3821): at android.os.Looper.loop(Looper.java:130)

    02-25 18:28:32.361: E/AndroidRuntime(3821): at android.app.ActivityThread.main(ActivityThread.java:3701)

    02-25 18:28:32.361: E/AndroidRuntime(3821): at java.lang.reflect.Method.invokeNative(Native Method)

    02-25 18:28:32.361: E/AndroidRuntime(3821): at java.lang.reflect.Method.invoke(Method.java:507)

    02-25 18:28:32.361: E/AndroidRuntime(3821): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)

    02-25 18:28:32.361: E/AndroidRuntime(3821): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:624)

    02-25 18:28:32.361: E/AndroidRuntime(3821): at dalvik.system.NativeStart.main(Native Method)

    02-25 18:28:32.361: E/AndroidRuntime(3821): Caused by: java.lang.Exception: Multiple instance of class AndroidDashActivity is NOT ALLOWED.

    02-25 18:28:32.361: E/AndroidRuntime(3821): at andrew.planetdash.AndroidDashActivity.<init>(AndroidDashActivity.java:41)

    02-25 18:28:32.361: E/AndroidRuntime(3821): at java.lang.Class.newInstanceImpl(Native Method)

    02-25 18:28:32.361: E/AndroidRuntime(3821): at java.lang.Class.newInstance(Class.java:1409)

    02-25 18:28:32.361: E/AndroidRuntime(3821): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)

    02-25 18:28:32.361: E/AndroidRuntime(3821): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1573)

    02-25 18:28:32.361: E/AndroidRuntime(3821): … 12 more

    [/patch]



    It does the same if I add super() to the start of the constructor.

    Does it have to do with the appClass = “start.MainClass”;?



    Also, comment if you think this post lack some information or layout or anything. I really tried this time normen.
@Addez said:
No reference to the constructor of my class exsist in any of my java files, so it must be jme3 that somehow calls it.

:brainsout:

You probably extend your own MainActivity class, namely in the "ComponentInfo" class.

I don’t have ComponentInfo in my project. Neither does Android harness…

Here is the whole message:

[java]02-25 19:17:02.967: D/shit(1340): Starting

02-25 19:17:03.157: I/JmeSystem(1340): INFO JmeAndroidSystem 19:17:03 Running on jMonkeyEngine 3.0.0 Beta

02-25 19:17:03.157: D/libEGL(1340): loaded /system/lib/egl/libGLES_android.so

02-25 19:17:03.207: D/libEGL(1340): loaded /system/lib/egl/libEGL_adreno200.so

02-25 19:17:03.227: D/libEGL(1340): loaded /system/lib/egl/libGLESv1_CM_adreno200.so

02-25 19:17:03.227: D/libEGL(1340): loaded /system/lib/egl/libGLESv2_adreno200.so

02-25 19:17:03.257: I/OGLESContext(1340): Display EGL Version: 1.0

02-25 19:17:03.268: I/AndroidConfigChooser(1340): JME3 using fastest EGL configuration available here:

02-25 19:17:03.268: I/AndroidConfigChooser(1340): JME3 using choosen config:

02-25 19:17:03.268: I/AndroidConfigChooser(1340): EGL_RED_SIZE = 5

02-25 19:17:03.268: I/AndroidConfigChooser(1340): EGL_GREEN_SIZE = 6

02-25 19:17:03.268: I/AndroidConfigChooser(1340): EGL_BLUE_SIZE = 5

02-25 19:17:03.268: I/AndroidConfigChooser(1340): EGL_ALPHA_SIZE = 0

02-25 19:17:03.268: I/AndroidConfigChooser(1340): EGL_DEPTH_SIZE = 16

02-25 19:17:03.268: I/AndroidConfigChooser(1340): EGL_STENCIL_SIZE = 0

02-25 19:17:03.268: I/AndroidConfigChooser(1340): EGL_RENDERABLE_TYPE = 7

02-25 19:17:03.268: I/AndroidConfigChooser(1340): EGL_SURFACE_TYPE = 1415

02-25 19:17:03.268: I/AndroidConfigChooser(1340): Using PixelFormat 4

02-25 19:17:03.277: I/AndroidHarness(1340): INFO AndroidHarness 19:17:03 Settings: Width 854 Height 480

02-25 19:17:03.277: I/AndroidHarness(1340): INFO AndroidHarness 19:17:03 Splash Screen Picture Resource ID: 0

02-25 19:17:03.277: I/AndroidHarness(1340): Splash Screen Skipped.

02-25 19:17:03.277: I/AndroidHarness(1340): onStart

02-25 19:17:03.287: I/AndroidHarness(1340): onResume

02-25 19:17:03.287: I/AndroidHarness(1340): onPause

02-25 19:17:03.287: I/AndroidHarness(1340): onStop

02-25 19:17:03.287: I/AndroidHarness(1340): onDestroy

02-25 19:17:03.297: D/shit(1340): Starting

02-25 19:17:03.297: D/AndroidRuntime(1340): Shutting down VM

02-25 19:17:03.297: W/dalvikvm(1340): threadid=1: thread exiting with uncaught exception (group=0x2aac8578)

02-25 19:17:03.297: E/AndroidRuntime(1340): FATAL EXCEPTION: main

02-25 19:17:03.297: E/AndroidRuntime(1340): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{andrew.planetdash/andrew.planetdash.AndroidDashActivity}: java.lang.Exception: Multiple instance of class AndroidDashActivity is NOT ALLOWED.

02-25 19:17:03.297: E/AndroidRuntime(1340): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1581)

02-25 19:17:03.297: E/AndroidRuntime(1340): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1675)

02-25 19:17:03.297: E/AndroidRuntime(1340): at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:2853)

02-25 19:17:03.297: E/AndroidRuntime(1340): at android.app.ActivityThread.access$1600(ActivityThread.java:121)

02-25 19:17:03.297: E/AndroidRuntime(1340): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:947)

02-25 19:17:03.297: E/AndroidRuntime(1340): at android.os.Handler.dispatchMessage(Handler.java:99)

02-25 19:17:03.297: E/AndroidRuntime(1340): at android.os.Looper.loop(Looper.java:130)

02-25 19:17:03.297: E/AndroidRuntime(1340): at android.app.ActivityThread.main(ActivityThread.java:3701)

02-25 19:17:03.297: E/AndroidRuntime(1340): at java.lang.reflect.Method.invokeNative(Native Method)

02-25 19:17:03.297: E/AndroidRuntime(1340): at java.lang.reflect.Method.invoke(Method.java:507)

02-25 19:17:03.297: E/AndroidRuntime(1340): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)

02-25 19:17:03.297: E/AndroidRuntime(1340): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:624)

02-25 19:17:03.297: E/AndroidRuntime(1340): at dalvik.system.NativeStart.main(Native Method)

02-25 19:17:03.297: E/AndroidRuntime(1340): Caused by: java.lang.Exception: Multiple instance of class AndroidDashActivity is NOT ALLOWED.

02-25 19:17:03.297: E/AndroidRuntime(1340): at andrew.planetdash.AndroidDashActivity.<init>(AndroidDashActivity.java:40)

02-25 19:17:03.297: E/AndroidRuntime(1340): at java.lang.Class.newInstanceImpl(Native Method)

02-25 19:17:03.297: E/AndroidRuntime(1340): at java.lang.Class.newInstance(Class.java:1409)

02-25 19:17:03.297: E/AndroidRuntime(1340): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)

02-25 19:17:03.297: E/AndroidRuntime(1340): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1573)

02-25 19:17:03.297: E/AndroidRuntime(1340): … 12 more

[/java]



Seems to me like it runs, pause, resumes and then quit itself just to start over. Is androidharness testing the whole program before running?

SOLUTION:

Because I had put it to landscape mode, android choose to kill the app, then restart it with other settings.

This will result in the double initiation.

To solve this:

-Open the manifest file called AndroidManifest.xml

Inside the <activity put the following line:

android:configChanges=“keyboardHidden|screenLayout|orientation”



Correct way:

[java]<activity

android:name=".AndroidDashActivity"

android:configChanges=“keyboardHidden|screenLayout|orientation”

android:label="@string/app_name" >

</activity>[/java]



Wrong way:

[java]<activity

android:name=".AndroidDashActivity"



android:label="@string/app_name" >

android:configChanges=“keyboardHidden|screenLayout|orientation”

</activity>[/java]

Everything looks correct to me. Once onDestroy() is called, that Activity object is no longer valid, so really you should nullify your static reference in the onDestroy() callback. What you did by putting the “configChanges” in your manifest is just delay your failure to when Android kills your app due to low memory and it has to be restarted, that’s when your exception will occur again.



To be honest there isn’t really a facility within jME3 to handle this sort of thing. If the app dies you can’t restore its state back, not without having some sort of “SaveGame” system which will need to be queried in the simpleInitApp() callback